fix for py/rna assigning an invalid index. also give better error message in this...
authorCampbell Barton <ideasman42@gmail.com>
Tue, 11 Oct 2011 05:45:59 +0000 (05:45 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 11 Oct 2011 05:45:59 +0000 (05:45 +0000)
source/blender/makesrna/intern/rna_ID.c
source/blender/python/intern/bpy_rna.c

index ddd0fcc1007ee69b4bfb546f7bd8478de8bed1f9..b366a23c19a1b0ff2bee6c46471007f9aba7cb15 100644 (file)
@@ -317,9 +317,15 @@ static int rna_IDPArray_length(PointerRNA *ptr)
 int rna_IDMaterials_assign_int(PointerRNA *ptr, int key, const PointerRNA *assign_ptr)
 {
        ID *id=           ptr->id.data;
 int rna_IDMaterials_assign_int(PointerRNA *ptr, int key, const PointerRNA *assign_ptr)
 {
        ID *id=           ptr->id.data;
+       short *totcol= give_totcolp_id(id);
        Material *mat_id= assign_ptr->id.data;
        Material *mat_id= assign_ptr->id.data;
-       assign_material_id(id, mat_id, key + 1);
-       return 1;
+       if(totcol && (key >= 0 && key < *totcol)) {
+               assign_material_id(id, mat_id, key + 1);
+               return 1;
+       }
+       else {
+               return 0;
+       }
 }
 
 #else
 }
 
 #else
index f5ecf91305d1a2b9832c4ace4ba717f756013c30..ba7e2d41e695d02fa99d4bcb33751d04b3ccf890 100644 (file)
@@ -1928,10 +1928,10 @@ static int pyrna_prop_collection_bool(BPy_PropertyRNA *self)
 }
 
 
 }
 
 
+/* notice getting the length of the collection is avoided unless negative
+ * index is used or to detect internal error with a valid index.
+ * This is done for faster lookups. */
 #define PYRNA_PROP_COLLECTION_ABS_INDEX(ret_err)                              \
 #define PYRNA_PROP_COLLECTION_ABS_INDEX(ret_err)                              \
-       /* notice getting the length of the collection is avoided unless negative \
-        * index is used or to detect internal error with a valid index.          \
-        * This is done for faster lookups. */                                    \
        if(keynum < 0) {                                                          \
                keynum_abs += RNA_property_collection_length(&self->ptr, self->prop); \
                if(keynum_abs < 0) {                                                  \
        if(keynum < 0) {                                                          \
                keynum_abs += RNA_property_collection_length(&self->ptr, self->prop); \
                if(keynum_abs < 0) {                                                  \
@@ -1984,11 +1984,18 @@ static int pyrna_prop_collection_ass_subscript_int(BPy_PropertyRNA *self, Py_ssi
        PYRNA_PROP_COLLECTION_ABS_INDEX(-1);
 
        if(RNA_property_collection_assign_int(&self->ptr, self->prop, keynum_abs, ptr) == 0) {
        PYRNA_PROP_COLLECTION_ABS_INDEX(-1);
 
        if(RNA_property_collection_assign_int(&self->ptr, self->prop, keynum_abs, ptr) == 0) {
+               const int len= RNA_property_collection_length(&self->ptr, self->prop);
+               if(keynum_abs >= len) {
+                       PyErr_Format(PyExc_IndexError,
+                                    "bpy_prop_collection[index] = value: "
+                                    "index %d out of range, size %d", keynum, len);
+               }
+               else {
 
 
-               PyErr_Format(PyExc_IndexError,
-                            "bpy_prop_collection[index] = value: "
-                            "failed assignment (unknown reason)", keynum);
-
+                       PyErr_Format(PyExc_IndexError,
+                                    "bpy_prop_collection[index] = value: "
+                                    "failed assignment (unknown reason)", keynum);
+               }
                return -1;
        }
 
                return -1;
        }