partial fix for [#32581] Mesh properties API does not allow for zeros in byte array
authorCampbell Barton <ideasman42@gmail.com>
Wed, 9 Jan 2013 13:25:46 +0000 (13:25 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 9 Jan 2013 13:25:46 +0000 (13:25 +0000)
bmesh access allows zero bytes, support still needs adding via RNA.

source/blender/makesdna/DNA_meshdata_types.h
source/blender/makesrna/intern/rna_mesh.c
source/blender/python/bmesh/bmesh_py_types_customdata.c

index 0c193e9be210f9e9451950c86d4e92ea6c2d0484..ffa5e420fcacd8fe2803ce7f1c462c13bbbe316e 100644 (file)
@@ -167,7 +167,7 @@ typedef struct MIntProperty {
        int             i;
 } MIntProperty;
 typedef struct MStringProperty {
        int             i;
 } MIntProperty;
 typedef struct MStringProperty {
-       char    s[256];
+       char    s[255], s_len;
 } MStringProperty;
 
 typedef struct OrigSpaceFace {
 } MStringProperty;
 
 typedef struct OrigSpaceFace {
index e471bf3dd3cd00663cdd75890b78404561c41ffe..d6ded23ffeda1a01a3c770d902556b3c331b39bd 100644 (file)
@@ -1233,6 +1233,26 @@ static char *rna_MeshStringProperty_path(PointerRNA *ptr)
        return rna_PolyCustomData_data_path(ptr, "layers_string", CD_PROP_STR);
 }
 
        return rna_PolyCustomData_data_path(ptr, "layers_string", CD_PROP_STR);
 }
 
+/* XXX, we dont have propper byte string support yet, so for now use the (bytes + 1)
+ * bmesh API exposes correct python/bytestring access */
+void rna_MeshStringProperty_s_get(PointerRNA *ptr, char *value)
+{
+       MStringProperty *ms = (MStringProperty *)ptr->data;
+       BLI_strncpy(value, ms->s, (int)ms->s_len + 1);
+}
+
+int rna_MeshStringProperty_s_length(PointerRNA *ptr)
+{
+       MStringProperty *ms = (MStringProperty *)ptr->data;
+       return (int)ms->s_len + 1;
+}
+
+void rna_MeshStringProperty_s_set(PointerRNA *ptr, const char *value)
+{
+       MStringProperty *ms = (MStringProperty *)ptr->data;
+       BLI_strncpy(ms->s, value, sizeof(ms->s));
+}
+
 static int rna_Mesh_tot_vert_get(PointerRNA *ptr)
 {
        Mesh *me = rna_mesh(ptr);
 static int rna_Mesh_tot_vert_get(PointerRNA *ptr)
 {
        Mesh *me = rna_mesh(ptr);
@@ -2161,6 +2181,7 @@ static void rna_def_mproperties(BlenderRNA *brna)
        /* low level mesh data access, treat as bytes */
        prop = RNA_def_property(srna, "value", PROP_STRING, PROP_BYTESTRING);
        RNA_def_property_string_sdna(prop, NULL, "s");
        /* low level mesh data access, treat as bytes */
        prop = RNA_def_property(srna, "value", PROP_STRING, PROP_BYTESTRING);
        RNA_def_property_string_sdna(prop, NULL, "s");
+       RNA_def_property_string_funcs(prop, "rna_MeshStringProperty_s_get", "rna_MeshStringProperty_s_length", "rna_MeshStringProperty_s_set");
        RNA_def_property_ui_text(prop, "Value", "");
        RNA_def_property_update(prop, 0, "rna_Mesh_update_data");
 }
        RNA_def_property_ui_text(prop, "Value", "");
        RNA_def_property_update(prop, 0, "rna_Mesh_update_data");
 }
index fd31f3c40cc299c5d6002c4a76b1a06aefc747c6..4a8f8d49f3545d1111468b05606ffd70cd7ce38a 100644 (file)
@@ -981,7 +981,7 @@ PyObject *BPy_BMLayerItem_GetItem(BPy_BMElem *py_ele, BPy_BMLayerItem *py_layer)
                case CD_PROP_STR:
                {
                        MStringProperty *mstring = value;
                case CD_PROP_STR:
                {
                        MStringProperty *mstring = value;
-                       ret = PyBytes_FromStringAndSize(mstring->s, BLI_strnlen(mstring->s, sizeof(mstring->s)));
+                       ret = PyBytes_FromStringAndSize(mstring->s, mstring->s_len);
                        break;
                }
                case CD_MTEXPOLY:
                        break;
                }
                case CD_MTEXPOLY:
@@ -1067,13 +1067,17 @@ int BPy_BMLayerItem_SetItem(BPy_BMElem *py_ele, BPy_BMLayerItem *py_layer, PyObj
                case CD_PROP_STR:
                {
                        MStringProperty *mstring = value;
                case CD_PROP_STR:
                {
                        MStringProperty *mstring = value;
-                       const char *tmp_val = PyBytes_AsString(py_value);
-                       if (UNLIKELY(tmp_val == NULL)) {
+                       char *tmp_val;
+                       Py_ssize_t tmp_val_len;
+                       if (UNLIKELY(PyBytes_AsStringAndSize(py_value, &tmp_val, &tmp_val_len) == -1)) {
                                PyErr_Format(PyExc_TypeError, "expected bytes, not a %.200s", Py_TYPE(py_value)->tp_name);
                                ret = -1;
                        }
                        else {
                                PyErr_Format(PyExc_TypeError, "expected bytes, not a %.200s", Py_TYPE(py_value)->tp_name);
                                ret = -1;
                        }
                        else {
-                               BLI_strncpy(mstring->s, tmp_val, min_ii(PyBytes_Size(py_value), sizeof(mstring->s)));
+                               if (tmp_val_len > sizeof(mstring->s))
+                                       tmp_val_len = sizeof(mstring->s);
+                               memcpy(mstring->s, tmp_val, tmp_val_len);
+                               mstring->s_len = tmp_val_len;
                        }
                        break;
                }
                        }
                        break;
                }