diff options
author | Campbell Barton <ideasman42@gmail.com> | 2015-07-29 02:31:53 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-07-29 03:49:34 +0300 |
commit | d226a4ba6df516147ee7a6f7fc40c9afad92fa49 (patch) | |
tree | 509b4bec942236940d6ebae1424c76e175071a51 /source | |
parent | 31cb14f5de10903d8d057ea539d5fea7fff8974a (diff) |
Fix leak in PyC_AsArray
Would only happen when the list-length was an unexpected size.
Also add PyC_AsArray_FAST
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/python/generic/py_capi_utils.c | 34 | ||||
-rw-r--r-- | source/blender/python/generic/py_capi_utils.h | 8 |
2 files changed, 27 insertions, 15 deletions
diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c index 1b72928b26e..17bd63bf5eb 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 ab630d1d203..93a3cb5b6d1 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); |