Examine result of SetList (and prevent sending NULL to PyList_SetItem)
authorNoel Power <noel.power@suse.com>
Fri, 25 Jan 2019 12:02:50 +0000 (12:02 +0000)
committerNoel Power <npower@samba.org>
Thu, 7 Feb 2019 12:44:30 +0000 (13:44 +0100)
Signed-off-by: Noel Power <noel.power@suse.com>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
lib/ldb/pyldb.c

index a4618db8d35e214e525f56e311eafb4e7b150cd5..1d4fe59f1031a60301532f772c1579227406a37b 100644 (file)
@@ -3430,17 +3430,41 @@ static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
        struct ldb_message *msg = pyldb_Message_AsMessage(self);
        Py_ssize_t i, j = 0;
        PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
+       if (l == NULL) {
+               return PyErr_NoMemory();
+       }
        if (msg->dn != NULL) {
+               PyObject *value = NULL;
                PyObject *obj = pyldb_Dn_FromDn(msg->dn);
-               PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", obj));
+               int res = 0;
+               value = Py_BuildValue("(sO)", "dn", obj);
                Py_CLEAR(obj);
+               if (value == NULL) {
+                       Py_CLEAR(l);
+                       return NULL;
+               }
+               res = PyList_SetItem(l, 0, value);
+               if (res == -1) {
+                       Py_CLEAR(l);
+                       return NULL;
+               }
                j++;
        }
        for (i = 0; i < msg->num_elements; i++, j++) {
+               PyObject *value = NULL;
                PyObject *py_el = PyLdbMessageElement_FromMessageElement(&msg->elements[i], msg->elements);
-               PyObject *value = Py_BuildValue("(sO)", msg->elements[i].name, py_el);
+               int res = 0;
                Py_CLEAR(py_el);
-               PyList_SetItem(l, j, value);
+               value = Py_BuildValue("(sO)", msg->elements[i].name, py_el);
+               if (value == NULL ) {
+                       Py_CLEAR(l);
+                       return NULL;
+               }
+               res = PyList_SetItem(l, 0, value);
+               if (res == -1) {
+                       Py_CLEAR(l);
+                       return NULL;
+               }
        }
        return l;
 }