From: Christian Seiler Date: Tue, 13 Aug 2013 19:36:58 +0000 (+0200) Subject: python: improve convert_tuple_to_char_pointer_array X-Git-Tag: lxc-1.0.0.alpha1~1^2~84 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b7f2846aabb8c1c59b078b4c529e60ea254432f1;p=thirdparty%2Flxc.git python: improve convert_tuple_to_char_pointer_array convert_tuple_to_char_pointer_array now also accepts lists and not only tuples when converting to a C array. Other fixes: - some checking that it's actually a list/tuple before trying to convert - off-by-a-few-bytes allocation error (sizeof(char *)*n+1 vs. sizeof(char *)*(n+1)/calloc(...)) Signed-off-by: Christian Seiler Acked-by: Serge E. Hallyn --- diff --git a/src/python-lxc/lxc.c b/src/python-lxc/lxc.c index 18f22248c..ec81bbf26 100644 --- a/src/python-lxc/lxc.c +++ b/src/python-lxc/lxc.c @@ -34,10 +34,18 @@ typedef struct { char** convert_tuple_to_char_pointer_array(PyObject *argv) { - int argc = PyTuple_GET_SIZE(argv); + int argc; int i, j; + + /* not a list or tuple */ + if (!PyList_Check(argv) && !PyTuple_Check(argv)) { + PyErr_SetString(PyExc_TypeError, "Expected list or tuple."); + return NULL; + } + + argc = PySequence_Fast_GET_SIZE(argv); - char **result = (char**) malloc(sizeof(char*)*argc + 1); + char **result = (char**) calloc(argc + 1, sizeof(char*)); if (result == NULL) { PyErr_SetNone(PyExc_MemoryError); @@ -45,7 +53,7 @@ convert_tuple_to_char_pointer_array(PyObject *argv) { } for (i = 0; i < argc; i++) { - PyObject *pyobj = PyTuple_GET_ITEM(argv, i); + PyObject *pyobj = PySequence_Fast_GET_ITEM(argv, i); assert(pyobj != NULL); char *str = NULL;