pyldb: avoid segfault when adding an element with no name
[nivanova/samba-autobuild/.git] / python / modules.c
index 371f0d5a07acb27121c1b1c851cea9f42bb90d02..ae91b0939aedd782d7837abcde17af8a1c2e3e7a 100644 (file)
 */
 
 #include <Python.h>
+#include "py3compat.h"
 #include "includes.h"
 #include "python/modules.h"
 #include "dynconfig/dynconfig.h"
 
 static bool PySys_PathPrepend(PyObject *list, const char *path)
 {
-       PyObject *py_path = PyString_FromString(path);
-       if (py_path == NULL)
+       bool ok;
+       PyObject *py_path = PyStr_FromString(path);
+       if (py_path == NULL) {
                return false;
-
-       return (PyList_Insert(list, 0, py_path) == 0);
+       }
+       ok = PyList_Insert(list, 0, py_path) == 0;
+       Py_XDECREF(py_path);
+       return ok;
 }
 
 bool py_update_path(void)
 {
-       PyObject *mod_sys, *py_path;
+       PyObject *mod_sys = NULL;
+       PyObject *py_path = NULL;
 
        mod_sys = PyImport_ImportModule("sys");
        if (mod_sys == NULL) {
@@ -42,22 +47,27 @@ bool py_update_path(void)
 
        py_path = PyObject_GetAttrString(mod_sys, "path");
        if (py_path == NULL) {
-               return false;
-       }       
+               goto error;
+       }
 
        if (!PyList_Check(py_path)) {
-               return false;
+               goto error;
        }
 
        if (!PySys_PathPrepend(py_path, dyn_PYTHONDIR)) {
-               return false;
+               goto error;
        }
 
        if (strcmp(dyn_PYTHONARCHDIR, dyn_PYTHONDIR) != 0) {
                if (!PySys_PathPrepend(py_path, dyn_PYTHONARCHDIR)) {
-                       return false;
+                       goto error;
                }
        }
-
+       Py_XDECREF(py_path);
+       Py_XDECREF(mod_sys);
        return true;
+error:
+       Py_XDECREF(py_path);
+       Py_XDECREF(mod_sys);
+       return false;
 }