[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [60810] trunk/blender/source/blender/ python/generic: add typechecks when assigning id-property arrays from python ( overflows and errors weren't detected)

Campbell Barton ideasman42 at gmail.com
Thu Oct 17 05:18:22 CEST 2013


Revision: 60810
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=60810
Author:   campbellbarton
Date:     2013-10-17 03:18:21 +0000 (Thu, 17 Oct 2013)
Log Message:
-----------
add typechecks when assigning id-property arrays from python (overflows and errors weren't detected)
reduce/simplify exceptions more.

Modified Paths:
--------------
    trunk/blender/source/blender/python/generic/idprop_py_api.c
    trunk/blender/source/blender/python/generic/idprop_py_api.h

Modified: trunk/blender/source/blender/python/generic/idprop_py_api.c
===================================================================
--- trunk/blender/source/blender/python/generic/idprop_py_api.c	2013-10-17 02:57:59 UTC (rev 60809)
+++ trunk/blender/source/blender/python/generic/idprop_py_api.c	2013-10-17 03:18:21 UTC (rev 60810)
@@ -346,6 +346,14 @@
 	if (name_obj) {
 		Py_ssize_t name_size;
 		name = _PyUnicode_AsStringAndSize(name_obj, &name_size);
+
+		if (name == NULL) {
+			PyErr_Format(PyExc_KeyError,
+			             "invalid id-property key, expected a string, not a %.200s",
+			             Py_TYPE(name_obj)->tp_name);
+			return false;
+		}
+
 		if (name_size > MAX_IDPROP_NAME) {
 			PyErr_SetString(PyExc_KeyError, "the length of IDProperty names is limited to 63 characters");
 			return false;
@@ -410,32 +418,47 @@
 
 		switch (val.array.type) {
 			case IDP_DOUBLE:
+			{
+				double *prop_data;
+
 				prop = IDP_New(IDP_ARRAY, &val, name);
+				prop_data = IDP_Array(prop);
 				for (i = 0; i < val.array.len; i++) {
 					item = PySequence_Fast_GET_ITEM(ob_seq_fast, i);
-					((double *)IDP_Array(prop))[i] = (float)PyFloat_AsDouble(item);
+					if (((prop_data[i] = PyFloat_AsDouble(item)) == -1.0) && PyErr_Occurred()) {
+						Py_DECREF(ob_seq_fast);
+						return false;
+					}
 				}
 				break;
+			}
 			case IDP_INT:
+			{
+				int *prop_data;
 				prop = IDP_New(IDP_ARRAY, &val, name);
+				prop_data = IDP_Array(prop);
 				for (i = 0; i < val.array.len; i++) {
 					item = PySequence_Fast_GET_ITEM(ob_seq_fast, i);
-					((int *)IDP_Array(prop))[i] = _PyLong_AsInt(item);
+					if (((prop_data[i] = _PyLong_AsInt(item)) == -1) && PyErr_Occurred()) {
+						Py_DECREF(ob_seq_fast);
+						return false;
+					}
 				}
 				break;
+			}
 			case IDP_IDPARRAY:
+			{
 				prop = IDP_NewIDPArray(name);
 				for (i = 0; i < val.array.len; i++) {
-					bool ok;
 					item = PySequence_Fast_GET_ITEM(ob_seq_fast, i);
-					ok = BPy_IDProperty_Map_ValidateAndCreate(NULL, prop, item);
 
-					if (ok == false) {
+					if (BPy_IDProperty_Map_ValidateAndCreate(NULL, prop, item) == false) {
 						Py_DECREF(ob_seq_fast);
-						return ok;
+						return false;
 					}
 				}
 				break;
+			}
 			default:
 				/* should never happen */
 				Py_DECREF(ob_seq_fast);
@@ -459,18 +482,6 @@
 		for (i = 0; i < len; i++) {
 			key = PySequence_GetItem(keys, i);
 			pval = PySequence_GetItem(vals, i);
-			if (!PyUnicode_Check(key)) {
-				IDP_FreeProperty(prop);
-				MEM_freeN(prop);
-				Py_XDECREF(keys);
-				Py_XDECREF(vals);
-				Py_XDECREF(key);
-				Py_XDECREF(pval);
-				PyErr_Format(PyExc_TypeError,
-				             "invalid id-property key type expected a string, not %.200s",
-				             Py_TYPE(key)->tp_name);
-				return false;
-			}
 			if (BPy_IDProperty_Map_ValidateAndCreate(key, prop, pval) == false) {
 				IDP_FreeProperty(prop);
 				MEM_freeN(prop);
@@ -527,11 +538,6 @@
 	else {
 		bool ok;
 
-		if (!PyUnicode_Check(key)) {
-			PyErr_SetString(PyExc_TypeError, "only strings are allowed as subgroup keys");
-			return -1;
-		}
-
 		ok = BPy_IDProperty_Map_ValidateAndCreate(key, prop, val);
 		if (ok == false) {
 			return -1;

Modified: trunk/blender/source/blender/python/generic/idprop_py_api.h
===================================================================
--- trunk/blender/source/blender/python/generic/idprop_py_api.h	2013-10-17 02:57:59 UTC (rev 60809)
+++ trunk/blender/source/blender/python/generic/idprop_py_api.h	2013-10-17 03:18:21 UTC (rev 60810)
@@ -71,7 +71,7 @@
 
 
 PyObject *BPy_IDGroup_WrapData(struct ID *id, struct IDProperty *prop, struct IDProperty *parent);
-const char *BPy_IDProperty_Map_ValidateAndCreate(PyObject *key, struct IDProperty *group, PyObject *ob);
+bool BPy_IDProperty_Map_ValidateAndCreate(PyObject *key, struct IDProperty *group, PyObject *ob);
 
 void IDProp_Init_Types(void);
 




More information about the Bf-blender-cvs mailing list