[Bf-blender-cvs] [d226a4b] master: Fix leak in PyC_AsArray

Campbell Barton noreply at git.blender.org
Wed Jul 29 02:54:31 CEST 2015


Commit: d226a4ba6df516147ee7a6f7fc40c9afad92fa49
Author: Campbell Barton
Date:   Wed Jul 29 09:31:53 2015 +1000
Branches: master
https://developer.blender.org/rBd226a4ba6df516147ee7a6f7fc40c9afad92fa49

Fix leak in PyC_AsArray

Would only happen when the list-length was an unexpected size.

Also add PyC_AsArray_FAST

===================================================================

M	source/blender/python/generic/py_capi_utils.c
M	source/blender/python/generic/py_capi_utils.h

===================================================================

diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c
index 1b72928..17bd63b 100644
--- a/source/blender/python/generic/py_capi_utils.c
+++ b/source/blender/python/generic/py_capi_utils.c
@@ -48,21 +48,16 @@
 #endif
 
 /* array utility function */
-int PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length,
-                const PyTypeObject *type, const bool is_double, const char *error_prefix)
+int PyC_AsArray_FAST(
+        void *array, PyObject *value_fast, const Py_ssize_t length,
+        const PyTypeObject *type, const bool is_double, const char *error_prefix)
 {
-	PyObject *value_fast;
-	Py_ssize_t value_len;
+	const Py_ssize_t value_len = PySequence_Fast_GET_SIZE(value_fast);
 	Py_ssize_t i;
 
-	if (!(value_fast = PySequence_Fast(value, error_prefix))) {
-		return -1;
-	}
-
-	value_len = PySequence_Fast_GET_SIZE(value_fast);
+	BLI_assert(PyList_Check(value_fast) || PyTuple_Check(value_fast));
 
 	if (value_len != length) {
-		Py_DECREF(value);
 		PyErr_Format(PyExc_TypeError,
 		             "%.200s: invalid sequence length. expected %d, got %d",
 		             error_prefix, length, value_len);
@@ -98,15 +93,12 @@ int PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length,
 		}
 	}
 	else {
-		Py_DECREF(value_fast);
 		PyErr_Format(PyExc_TypeError,
 		             "%s: internal error %s is invalid",
 		             error_prefix, type->tp_name);
 		return -1;
 	}
 
-	Py_DECREF(value_fast);
-
 	if (PyErr_Occurred()) {
 		PyErr_Format(PyExc_TypeError,
 		             "%s: one or more items could not be used as a %s",
@@ -117,6 +109,22 @@ int PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length,
 	return 0;
 }
 
+int PyC_AsArray(
+        void *array, PyObject *value, const Py_ssize_t length,
+        const PyTypeObject *type, const bool is_double, const char *error_prefix)
+{
+	PyObject *value_fast;
+	int ret;
+
+	if (!(value_fast = PySequence_Fast(value, error_prefix))) {
+		return -1;
+	}
+
+	ret = PyC_AsArray_FAST(array, value_fast, length, type, is_double, error_prefix);
+	Py_DECREF(value_fast);
+	return ret;
+}
+
 /* array utility function */
 PyObject *PyC_FromArray(const void *array, int length, const PyTypeObject *type,
                         const bool is_double, const char *error_prefix)
diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h
index ab630d1..93a3cb5 100644
--- a/source/blender/python/generic/py_capi_utils.h
+++ b/source/blender/python/generic/py_capi_utils.h
@@ -38,8 +38,12 @@ PyObject *		PyC_FrozenSetFromStrings(const char **strings);
 PyObject *		PyC_Err_Format_Prefix(PyObject *exception_type_prefix, const char *format, ...);
 void			PyC_FileAndNum(const char **filename, int *lineno);
 void			PyC_FileAndNum_Safe(const char **filename, int *lineno); /* checks python is running */
-int				PyC_AsArray(void *array, PyObject *value, const Py_ssize_t length,
-                            const PyTypeObject *type, const bool is_double, const char *error_prefix);
+int             PyC_AsArray_FAST(
+        void *array, PyObject *value_fast, const Py_ssize_t length,
+        const PyTypeObject *type, const bool is_double, const char *error_prefix);
+int             PyC_AsArray(
+        void *array, PyObject *value, const Py_ssize_t length,
+        const PyTypeObject *type, const bool is_double, const char *error_prefix);
 PyObject *      PyC_FromArray(const void *array, int length, const PyTypeObject *type,
                               const bool is_double, const char *error_prefix);
 void            PyC_Tuple_Fill(PyObject *tuple, PyObject *value);




More information about the Bf-blender-cvs mailing list