<div dir="ltr">Hi,<div><br></div><div style>I'm getting this on ubuntu 12.10 with this</div><div style><br></div><div style>[...]</div><div style><br></div><div style><div>make[3]: Entering directory `/home/caglar/Projects/lxc-upstream/src/python-lxc'</div>

<div>CFLAGS="-g -O2 -Wall -Werror -I ../../src -L../../src/lxc/" /usr/bin/python3 setup.py build</div><div>running build</div><div>running build_py</div><div>creating build</div><div>creating build/lib.linux-x86_64-3.2</div>

<div>creating build/lib.linux-x86_64-3.2/lxc</div><div>copying lxc/__init__.py -> build/lib.linux-x86_64-3.2/lxc</div><div>running build_ext</div><div>building '_lxc' extension</div><div>creating build/temp.linux-x86_64-3.2</div>

<div>gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -O2 -Wall -Werror -I ../../src -L../../src/lxc/ -fPIC -I/usr/include/python3.2mu -c lxc.c -o build/temp.linux-x86_64-3.2/lxc.o</div><div>lxc.c: In function ‘convert_tuple_to_char_pointer_array’:</div>

<div>lxc.c:58:9: error: implicit declaration of function ‘PyUnicode_AsUTF8’ [-Werror=implicit-function-declaration]</div><div>lxc.c:58:13: error: assignment makes pointer from integer without a cast [-Werror]</div><div>cc1: all warnings being treated as errors</div>

<div>error: command 'gcc' failed with exit status 1</div><div>make[3]: *** [all] Error 1</div><div>make[3]: Leaving directory `/home/caglar/Projects/lxc-upstream/src/python-lxc'</div><div>make[2]: *** [all-recursive] Error 1</div>

<div>make[2]: Leaving directory `/home/caglar/Projects/lxc-upstream/src'</div><div>make[1]: *** [all] Error 2</div><div>make[1]: Leaving directory `/home/caglar/Projects/lxc-upstream/src'</div><div>make: *** [all-recursive] Error 1</div>

<div><br></div><div style>and quick grep shows no trace of PyUnicode_AsUTF8 but PyUnicode_AsUTF8String function</div><div style><br></div><div style><div>[caglar@qgq:~/Projects/lxc-upstream(devel)] grep -r PyUnicode_AsUTF8 /usr/include/python3.2</div>

<div>/usr/include/python3.2/unicodeobject.h:# define PyUnicode_AsUTF8String PyUnicodeUCS2_AsUTF8String</div><div>/usr/include/python3.2/unicodeobject.h:# define PyUnicode_AsUTF8String PyUnicodeUCS4_AsUTF8String</div><div>

/usr/include/python3.2/unicodeobject.h:   Same as PyUnicode_AsUTF8String() except</div><div>/usr/include/python3.2/unicodeobject.h:   *** please use PyUnicode_AsUTF8String() instead.</div><div>/usr/include/python3.2/unicodeobject.h:   *** please use PyUnicode_AsUTF8String() instead.</div>

<div>/usr/include/python3.2/unicodeobject.h:PyAPI_FUNC(PyObject*) PyUnicode_AsUTF8String(</div></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Apr 23, 2013 at 6:52 PM, Stéphane Graber <span dir="ltr"><<a href="mailto:stgraber@ubuntu.com" target="_blank">stgraber@ubuntu.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This finally fixes a few issues with the magic<br>
convert_tuple_to_char_pointer_array function.<br>
<br>
This now clearly copies the char* from the python object so we don't<br>
end up keeping reference to those.<br>
<br>
Also add the few required free calls to free the content of the array.<br>
<br>
Signed-off-by: Stéphane Graber <<a href="mailto:stgraber@ubuntu.com">stgraber@ubuntu.com</a>><br>
---<br>
 src/python-lxc/lxc.c | 51 ++++++++++++++++++++++++++++++++++++---------------<br>
 1 file changed, 36 insertions(+), 15 deletions(-)<br>
<br>
diff --git a/src/python-lxc/lxc.c b/src/python-lxc/lxc.c<br>
index 85bab11..85710d6 100644<br>
--- a/src/python-lxc/lxc.c<br>
+++ b/src/python-lxc/lxc.c<br>
@@ -35,7 +35,7 @@ typedef struct {<br>
 char**<br>
 convert_tuple_to_char_pointer_array(PyObject *argv) {<br>
     int argc = PyTuple_GET_SIZE(argv);<br>
-    int i;<br>
+    int i, j;<br>
<br>
     char **result = (char**) malloc(sizeof(char*)*argc + 1);<br>
<br>
@@ -49,30 +49,46 @@ convert_tuple_to_char_pointer_array(PyObject *argv) {<br>
         assert(pyobj != NULL);<br>
<br>
         char *str = NULL;<br>
-        PyObject *pystr = NULL;<br>
+<br>
         if (!PyUnicode_Check(pyobj)) {<br>
             PyErr_SetString(PyExc_ValueError, "Expected a string");<br>
-            free(result);<br>
-            return NULL;<br>
+            goto error;<br>
         }<br>
<br>
-        pystr = PyUnicode_AsUTF8String(pyobj);<br>
-        if (pystr == NULL) {<br>
-            PyErr_SetString(PyExc_ValueError, "Unable to convert to UTF-8");<br>
-            free(result);<br>
-            return NULL;<br>
+        str = PyUnicode_AsUTF8(pyobj);<br>
+        if (!str) {<br>
+            /* Maybe it wasn't UTF-8 encoded.  An exception is already set. */<br>
+            goto error;<br>
         }<br>
<br>
-        str = PyBytes_AsString(pystr);<br>
-        assert(str != NULL);<br>
+        /* We must make a copy of str, because it points into internal memory<br>
+         * which we do not own.  Assume it's NULL terminated, otherwise we'd<br>
+         * have to use PyUnicode_AsUTF8AndSize() and be explicit about copying<br>
+         * the memory.<br>
+         */<br>
+        result[i] = strdup(str);<br>
<br>
-        memcpy((char *) &result[i], (char *) &str, sizeof(str));<br>
-        Py_DECREF(pystr);<br>
+        /* Do not decref pyobj since we stole a reference by using<br>
+         * PyTuple_GET_ITEM().<br>
+         */<br>
+        if (result[i] == NULL) {<br>
+            PyErr_SetNone(PyExc_MemoryError);<br>
+            goto error;<br>
+        }<br>
     }<br>
<br>
     result[argc] = NULL;<br>
-<br>
     return result;<br>
+<br>
+error:<br>
+    /* We can only iterate up to but not including i because malloc() does not<br>
+     * initialize its memory.  Thus if we got here, i points to the index<br>
+     * after the last strdup'd entry in result.<br>
+     */<br>
+    for (j = 0; j < i; j++)<br>
+        free(result[j]);<br>
+    free(result);<br>
+    return NULL;<br>
 }<br>
<br>
 static void<br>
@@ -203,6 +219,7 @@ Container_create(Container *self, PyObject *args, PyObject *kwds)<br>
     char* template_name = NULL;<br>
     char** create_args = {NULL};<br>
     PyObject *retval = NULL, *vargs = NULL;<br>
+    int i = 0;<br>
     static char *kwlist[] = {"template", "args", NULL};<br>
<br>
     if (! PyArg_ParseTupleAndKeywords(args, kwds, "s|O", kwlist,<br>
@@ -231,6 +248,8 @@ Container_create(Container *self, PyObject *args, PyObject *kwds)<br>
         /* We cannot have gotten here unless vargs was given and create_args<br>
          * was successfully allocated.<br>
          */<br>
+        for (i = 0; i < PyTuple_GET_SIZE(vargs); i++)<br>
+            free(create_args[i]);<br>
         free(create_args);<br>
     }<br>
<br>
@@ -495,7 +514,7 @@ Container_start(Container *self, PyObject *args, PyObject *kwds)<br>
 {<br>
     char** init_args = {NULL};<br>
     PyObject *useinit = NULL, *retval = NULL, *vargs = NULL;<br>
-    int init_useinit = 0;<br>
+    int init_useinit = 0, i = 0;<br>
     static char *kwlist[] = {"useinit", "cmd", NULL};<br>
<br>
     if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist,<br>
@@ -524,6 +543,8 @@ Container_start(Container *self, PyObject *args, PyObject *kwds)<br>
         /* We cannot have gotten here unless vargs was given and create_args<br>
          * was successfully allocated.<br>
          */<br>
+        for (i = 0; i < PyTuple_GET_SIZE(vargs); i++)<br>
+            free(init_args[i]);<br>
         free(init_args);<br>
     }<br>
<br>
--<br>
1.8.1.2<br>
<br>
<br>
------------------------------------------------------------------------------<br>
Try New Relic Now & We'll Send You this Cool Shirt<br>
New Relic is the only SaaS-based application performance monitoring service<br>
that delivers powerful full stack analytics. Optimize and monitor your<br>
browser, app, & servers with just a few lines of code. Try New Relic<br>
and get this awesome Nerd Life shirt! <a href="http://p.sf.net/sfu/newrelic_d2d_apr" target="_blank">http://p.sf.net/sfu/newrelic_d2d_apr</a><br>
_______________________________________________<br>
Lxc-devel mailing list<br>
<a href="mailto:Lxc-devel@lists.sourceforge.net">Lxc-devel@lists.sourceforge.net</a><br>
<a href="https://lists.sourceforge.net/lists/listinfo/lxc-devel" target="_blank">https://lists.sourceforge.net/lists/listinfo/lxc-devel</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>S.Çağlar Onur <<a href="mailto:caglar@10ur.org">caglar@10ur.org</a>>
</div>