modify my last commit to fix [#22486] add_actuator crashes when name is bigger than 32 chars

Throwing an exception if the strings too long means scripts need to be aware of string lengths and changing a string length in RNA can too easily break scripts.
Instead honor the string length in RNA_property_string_set()
This commit is contained in:
Campbell Barton 2010-06-12 17:30:21 +00:00
parent c3c6fb2de2
commit 971e4be108
4 changed files with 43 additions and 20 deletions

@ -71,7 +71,8 @@ void IDP_FreeArray(struct IDProperty *prop);
void IDP_UnlinkArray(struct IDProperty *prop);
/* ---------- String Type ------------ */
void IDP_AssignString(struct IDProperty *prop, char *st);
IDProperty *IDP_NewString(const char *st, const char *name, int maxlen);/* maxlen excludes '\0' */
void IDP_AssignString(struct IDProperty *prop, char *st, int maxlen); /* maxlen excludes '\0' */
void IDP_ConcatStringC(struct IDProperty *prop, char *st);
void IDP_ConcatString(struct IDProperty *str1, struct IDProperty *append);
void IDP_FreeString(struct IDProperty *prop);

@ -299,6 +299,34 @@ IDProperty *IDP_CopyArray(IDProperty *prop)
/* ---------- String Type ------------ */
IDProperty *IDP_NewString(const char *st, const char *name, int maxlen)
{
IDProperty *prop = MEM_callocN(sizeof(IDProperty), "IDProperty string");
if (st == NULL) {
prop->data.pointer = MEM_callocN(DEFAULT_ALLOC_FOR_NULL_STRINGS, "id property string 1");
prop->totallen = DEFAULT_ALLOC_FOR_NULL_STRINGS;
prop->len = 1; /*NULL string, has len of 1 to account for null byte.*/
}
else {
int stlen = strlen(st);
if(maxlen > 0 && maxlen < stlen)
stlen = maxlen;
stlen++; /* null terminator '\0' */
prop->data.pointer = MEM_callocN(stlen, "id property string 2");
prop->len = prop->totallen = stlen;
BLI_strncpy(prop->data.pointer, st, stlen);
}
prop->type = IDP_STRING;
BLI_strncpy(prop->name, name, MAX_IDPROP_NAME);
return prop;
}
IDProperty *IDP_CopyString(IDProperty *prop)
{
IDProperty *newp = idp_generic_copy(prop);
@ -312,14 +340,19 @@ IDProperty *IDP_CopyString(IDProperty *prop)
}
void IDP_AssignString(IDProperty *prop, char *st)
void IDP_AssignString(IDProperty *prop, char *st, int maxlen)
{
int stlen;
stlen = strlen(st);
IDP_ResizeArray(prop, stlen+1); /*make room for null byte :) */
strcpy(prop->data.pointer, st);
if(maxlen > 0 && maxlen < stlen)
stlen= maxlen;
stlen++; /* make room for null byte */
IDP_ResizeArray(prop, stlen);
BLI_strncpy(prop->data.pointer, st, stlen);
}
void IDP_ConcatStringC(IDProperty *prop, char *st)
@ -709,9 +742,6 @@ IDProperty *IDP_New(int type, IDPropertyTemplate val, const char *name)
prop->type = type;
BLI_strncpy(prop->name, name, MAX_IDPROP_NAME);
/*security null byte*/
prop->name[MAX_IDPROP_NAME-1] = 0;
return prop;
}

@ -961,7 +961,7 @@ int RNA_property_int_clamp(PointerRNA *ptr, PropertyRNA *prop, int *value)
}
/* this is the max length including \0 terminator.
* -1 used when their is no maximum */
* '0' used when their is no maximum */
int RNA_property_string_maxlength(PropertyRNA *prop)
{
StringPropertyRNA *sprop= (StringPropertyRNA*)rna_ensure_property(prop);
@ -1830,18 +1830,15 @@ void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *val
IDProperty *idprop;
if((idprop=rna_idproperty_check(&prop, ptr)))
IDP_AssignString(idprop, (char*)value);
IDP_AssignString(idprop, (char*)value, RNA_property_string_maxlength(prop) - 1);
else if(sprop->set)
sprop->set(ptr, value);
sprop->set(ptr, value); /* set function needs to clamp its self */
else if(prop->flag & PROP_EDITABLE) {
IDPropertyTemplate val = {0};
IDProperty *group;
val.str= (char*)value;
group= RNA_struct_idproperties(ptr, 1);
if(group)
IDP_AddToGroup(group, IDP_New(IDP_STRING, val, (char*)prop->identifier));
IDP_AddToGroup(group, IDP_NewString((char*)value, (char*)prop->identifier, RNA_property_string_maxlength(prop) - 1));
}
}

@ -924,16 +924,11 @@ int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, ParameterList *parms, v
}
case PROP_STRING:
{
Py_ssize_t param_len;
int param_maxlen= RNA_property_string_maxlength(prop) - 1;
char *param = _PyUnicode_AsStringAndSize(value, &param_len);
char *param = _PyUnicode_AsString(value);
if (param==NULL) {
PyErr_Format(PyExc_TypeError, "%.200s %.200s.%.200s expected a string type", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop));
return -1;
} else if (param_maxlen != -1 && param_len > param_maxlen) { /* -1 because it includes the \0 */
PyErr_Format(PyExc_TypeError, "%.200s %.200s.%.200s string is length %d, expected maximum of %d", error_prefix, RNA_struct_identifier(ptr->type), RNA_property_identifier(prop), (int)param_len, param_maxlen);
return -1;
}
else {
if(data) *((char**)data)= param;