Blob Blame Raw
From 13ba25fa006ae1af1b3a7507cefc9fb433859803 Mon Sep 17 00:00:00 2001
Message-Id: <13ba25fa006ae1af1b3a7507cefc9fb433859803@dist-git>
From: Jiri Denemark <jdenemar@redhat.com>
Date: Mon, 3 Aug 2015 10:26:26 +0200
Subject: [PATCH] Check return value of PyList_Append

libvirt_virDomainGetSecurityLabelList called PyList_Append without
checking its return value. While looking at it I noticed the function
did not properly check several other return values either so I fixed
them all.

https://bugzilla.redhat.com/show_bug.cgi?id=1249511

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
(cherry picked from commit 5a6b2c98390a2344c9c3cc10a1c346a4838f0d58)
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 libvirt-override.c | 46 ++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 38 insertions(+), 8 deletions(-)

diff --git a/libvirt-override.c b/libvirt-override.c
index 2398228..4dfe332 100644
--- a/libvirt-override.c
+++ b/libvirt-override.c
@@ -3144,32 +3144,62 @@ libvirt_virDomainGetSecurityLabel(PyObject *self ATTRIBUTE_UNUSED, PyObject *arg
 
 #if LIBVIR_CHECK_VERSION(0, 10, 0)
 static PyObject *
-libvirt_virDomainGetSecurityLabelList(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
+libvirt_virDomainGetSecurityLabelList(PyObject *self ATTRIBUTE_UNUSED,
+                                      PyObject *args)
+{
     PyObject *py_retval;
     int c_retval;
     virDomainPtr dom;
     PyObject *pyobj_dom;
-    virSecurityLabel *labels;
+    virSecurityLabel *labels = NULL;
     size_t i;
 
     if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetSecurityLabel", &pyobj_dom))
         return NULL;
+
     dom = (virDomainPtr) PyvirDomain_Get(pyobj_dom);
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
     c_retval = virDomainGetSecurityLabelList(dom, &labels);
     LIBVIRT_END_ALLOW_THREADS;
+
     if (c_retval < 0)
         return VIR_PY_NONE;
-    py_retval = PyList_New(0);
+
+    if (!(py_retval = PyList_New(0)))
+        goto error;
+
     for (i = 0 ; i < c_retval ; i++) {
-        PyObject *entry = PyList_New(2);
-        PyList_SetItem(entry, 0, libvirt_constcharPtrWrap(&labels[i].label[0]));
-        PyList_SetItem(entry, 1, libvirt_boolWrap(labels[i].enforcing));
-        PyList_Append(py_retval, entry);
+        PyObject *entry;
+        PyObject *value;
+
+        if (!(entry = PyList_New(2)) ||
+            PyList_Append(py_retval, entry) < 0) {
+            Py_XDECREF(entry);
+            goto error;
+        }
+
+        if (!(value = libvirt_constcharPtrWrap(&labels[i].label[0])) ||
+            PyList_SetItem(entry, 0, value) < 0) {
+            Py_XDECREF(value);
+            goto error;
+        }
+
+        if (!(value = libvirt_boolWrap(labels[i].enforcing)) ||
+            PyList_SetItem(entry, 1, value) < 0) {
+            Py_XDECREF(value);
+            goto error;
+        }
     }
-    free(labels);
+
+ cleanup:
+    VIR_FREE(labels);
     return py_retval;
+
+ error:
+    Py_XDECREF(py_retval);
+    py_retval = NULL;
+    goto cleanup;
 }
 #endif /* LIBVIR_CHECK_VERSION(0, 10, 0) */
 
-- 
2.5.0