From 971e4be1081fb655f80d6d4065c734ccf625c987 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 12 Jun 2010 17:30:21 +0000 Subject: [PATCH] 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() --- source/blender/blenkernel/BKE_idprop.h | 3 +- source/blender/blenkernel/intern/idprop.c | 42 ++++++++++++++++++--- source/blender/makesrna/intern/rna_access.c | 11 ++---- source/blender/python/intern/bpy_rna.c | 7 +--- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/source/blender/blenkernel/BKE_idprop.h b/source/blender/blenkernel/BKE_idprop.h index e33239293a8..0e0d76f4284 100644 --- a/source/blender/blenkernel/BKE_idprop.h +++ b/source/blender/blenkernel/BKE_idprop.h @@ -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); diff --git a/source/blender/blenkernel/intern/idprop.c b/source/blender/blenkernel/intern/idprop.c index 98b3522059c..2ccb33b088a 100644 --- a/source/blender/blenkernel/intern/idprop.c +++ b/source/blender/blenkernel/intern/idprop.c @@ -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; } diff --git a/source/blender/makesrna/intern/rna_access.c b/source/blender/makesrna/intern/rna_access.c index 75980e52206..4673f23b1ae 100644 --- a/source/blender/makesrna/intern/rna_access.c +++ b/source/blender/makesrna/intern/rna_access.c @@ -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)); } } diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 0d1bb64bac2..51c8fea0c8e 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -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, ¶m_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;