[lxc-devel] [PATCH 6/8] python: improve convert_tuple_to_char_pointer_array
Serge Hallyn
serge.hallyn at ubuntu.com
Wed Aug 14 20:36:08 UTC 2013
Quoting Christian Seiler (christian at iwakd.de):
> 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 <christian at iwakd.de>
Acked-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>
> ---
> src/python-lxc/lxc.c | 14 +++++++++++---
> 1 file changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/src/python-lxc/lxc.c b/src/python-lxc/lxc.c
> index 18f2224..ec81bbf 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;
> --
> 1.7.10.4
>
>
> ------------------------------------------------------------------------------
> Get 100% visibility into Java/.NET code with AppDynamics Lite!
> It's a free troubleshooting tool designed for production.
> Get down to code-level detail for bottlenecks, with <2% overhead.
> Download for free and get started troubleshooting in minutes.
> http://pubads.g.doubleclick.net/gampad/clk?id=48897031&iu=/4140/ostg.clktrk
> _______________________________________________
> Lxc-devel mailing list
> Lxc-devel at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/lxc-devel
More information about the lxc-devel
mailing list