[PATCH 2/2] python: add error checking for calls to PyList_New

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Giuseppe Scrivano <gscrivan@xxxxxxxxxx>
---
 python/libvirt-override.c | 88 +++++++++++++++++++++++++++++++++++++----------
 1 file changed, 70 insertions(+), 18 deletions(-)

diff --git a/python/libvirt-override.c b/python/libvirt-override.c
index 83bca94..3b902bc 100644
--- a/python/libvirt-override.c
+++ b/python/libvirt-override.c
@@ -2095,7 +2095,7 @@ static int virConnectCredCallbackWrapper(virConnectCredentialPtr cred,
     PyObject *pyauth = (PyObject *)cbdata;
     PyObject *pycbdata;
     PyObject *pycb;
-    PyObject *pyret;
+    PyObject *pyret = NULL;
     int ret = -1;
     size_t i;
 
@@ -2108,7 +2108,8 @@ static int virConnectCredCallbackWrapper(virConnectCredentialPtr cred,
     pycred = PyTuple_New(ncred);
     for (i = 0; i < ncred; i++) {
         PyObject *pycreditem;
-        pycreditem = PyList_New(5);
+        if (!(pycreditem = PyList_New(5)))
+            goto cleanup;
         Py_INCREF(Py_None);
         PyTuple_SetItem(pycred, i, pycreditem);
         PyList_SET_ITEM(pycreditem, 0, PyInt_FromLong((long) cred[i].type));
@@ -2382,7 +2383,10 @@ libvirt_virConnectListDomainsID(PyObject *self ATTRIBUTE_UNUSED,
             return VIR_PY_NONE;
         }
     }
-    py_retval = PyList_New(c_retval);
+    if (!(py_retval = PyList_New(c_retval))) {
+        VIR_FREE(ids);
+        return VIR_PY_NONE;
+    }
 
     if (ids) {
         for (i = 0; i < c_retval; i++) {
@@ -2472,7 +2476,12 @@ libvirt_virConnectListDefinedDomains(PyObject *self ATTRIBUTE_UNUSED,
             return VIR_PY_NONE;
         }
     }
-    py_retval = PyList_New(c_retval);
+    if (!(py_retval = PyList_New(c_retval))) {
+        for (i = 0; i < c_retval; i++)
+            VIR_FREE(names[i]);
+        VIR_FREE(names);
+        return VIR_PY_NONE;
+    }
 
     if (names) {
         for (i = 0; i < c_retval; i++) {
@@ -2621,7 +2630,8 @@ libvirt_virDomainSnapshotListChildrenNames(PyObject *self ATTRIBUTE_UNUSED,
             return VIR_PY_NONE;
         }
     }
-    py_retval = PyList_New(c_retval);
+    if (!(py_retval = PyList_New(c_retval)))
+        goto cleanup;
 
     for (i = 0; i < c_retval; i++) {
         if ((pyobj_snap = libvirt_constcharPtrWrap(names[i])) == NULL) {
@@ -2724,7 +2734,8 @@ libvirt_virDomainGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
     LIBVIRT_END_ALLOW_THREADS;
     if (c_retval < 0)
         return VIR_PY_NONE;
-    py_retval = PyList_New(5);
+    if (!(py_retval = PyList_New(5)))
+        return VIR_PY_NONE;
     PyList_SET_ITEM(py_retval, 0, libvirt_intWrap((int) info.state));
     PyList_SET_ITEM(py_retval, 1, libvirt_ulongWrap(info.maxMem));
     PyList_SET_ITEM(py_retval, 2, libvirt_ulongWrap(info.memory));
@@ -2757,7 +2768,9 @@ libvirt_virDomainGetState(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
     if (c_retval < 0)
         return VIR_PY_NONE;
 
-    py_retval = PyList_New(2);
+    if (!(py_retval = PyList_New(2)))
+        return VIR_PY_NONE;
+
     PyList_SET_ITEM(py_retval, 0, libvirt_intWrap(state));
     PyList_SET_ITEM(py_retval, 1, libvirt_intWrap(reason));
     return py_retval;
@@ -2782,7 +2795,8 @@ libvirt_virDomainGetControlInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
     LIBVIRT_END_ALLOW_THREADS;
     if (c_retval < 0)
         return VIR_PY_NONE;
-    py_retval = PyList_New(3);
+    if (!(py_retval = PyList_New(3)))
+        return VIR_PY_NONE;
     PyList_SET_ITEM(py_retval, 0, libvirt_intWrap(info.state));
     PyList_SET_ITEM(py_retval, 1, libvirt_intWrap(info.details));
     PyList_SET_ITEM(py_retval, 2, libvirt_longlongWrap(info.stateTime));
@@ -2808,7 +2822,9 @@ libvirt_virDomainGetBlockInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
     LIBVIRT_END_ALLOW_THREADS;
     if (c_retval < 0)
         return VIR_PY_NONE;
-    py_retval = PyList_New(3);
+    if (!(py_retval = PyList_New(3)))
+        return VIR_PY_NONE;
+
     PyList_SET_ITEM(py_retval, 0, libvirt_ulonglongWrap(info.capacity));
     PyList_SET_ITEM(py_retval, 1, libvirt_ulonglongWrap(info.allocation));
     PyList_SET_ITEM(py_retval, 2, libvirt_ulonglongWrap(info.physical));
@@ -2832,7 +2848,9 @@ libvirt_virNodeGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
     LIBVIRT_END_ALLOW_THREADS;
     if (c_retval < 0)
         return VIR_PY_NONE;
-    py_retval = PyList_New(8);
+    if (!(py_retval = PyList_New(8)))
+        return VIR_PY_NONE;
+
     PyList_SET_ITEM(py_retval, 0, libvirt_constcharPtrWrap(&info.model[0]));
     PyList_SET_ITEM(py_retval, 1, libvirt_longWrap((long) info.memory >> 10));
     PyList_SET_ITEM(py_retval, 2, libvirt_intWrap((int) info.cpus));
@@ -2952,7 +2970,12 @@ libvirt_virConnectListNetworks(PyObject *self ATTRIBUTE_UNUSED,
             return VIR_PY_NONE;
         }
     }
-    py_retval = PyList_New(c_retval);
+    if (!(py_retval = PyList_New(c_retval))) {
+        for (i = 0; i < c_retval; i++)
+            VIR_FREE(names[i]);
+        VIR_FREE(names);
+        return VIR_PY_NONE;
+    }
 
     if (names) {
         for (i = 0; i < c_retval; i++) {
@@ -2998,7 +3021,12 @@ libvirt_virConnectListDefinedNetworks(PyObject *self ATTRIBUTE_UNUSED,
             return VIR_PY_NONE;
         }
     }
-    py_retval = PyList_New(c_retval);
+    if (!(py_retval = PyList_New(c_retval))) {
+        for (i = 0; i < c_retval; i++)
+            VIR_FREE(names[i]);
+        VIR_FREE(names);
+        return VIR_PY_NONE;
+    }
 
     if (names) {
         for (i = 0; i < c_retval; i++) {
@@ -3207,11 +3235,14 @@ libvirt_virNodeGetCellsFreeMemory(PyObject *self ATTRIBUTE_UNUSED, PyObject *arg
         VIR_FREE(freeMems);
         return VIR_PY_NONE;
     }
-    py_retval = PyList_New(c_retval);
+    if (!(py_retval = PyList_New(c_retval)))
+        goto cleanup;
+
     for (i = 0; i < c_retval; i++) {
         PyList_SET_ITEM(py_retval, i,
                 libvirt_longlongWrap((long long) freeMems[i]));
     }
+cleanup:
     VIR_FREE(freeMems);
     return py_retval;
 }
@@ -3790,7 +3821,12 @@ libvirt_virNodeListDevices(PyObject *self ATTRIBUTE_UNUSED,
             return VIR_PY_NONE;
         }
     }
-    py_retval = PyList_New(c_retval);
+    if (!(py_retval = PyList_New(c_retval))) {
+        for (i = 0; i < c_retval; i++)
+            VIR_FREE(names[i]);
+        VIR_FREE(names);
+        return VIR_PY_NONE;
+    }
 
     if (names) {
         for (i = 0; i < c_retval; i++) {
@@ -3880,7 +3916,12 @@ libvirt_virNodeDeviceListCaps(PyObject *self ATTRIBUTE_UNUSED,
             return VIR_PY_NONE;
         }
     }
-    py_retval = PyList_New(c_retval);
+    if (!(py_retval = PyList_New(c_retval))) {
+        for (i = 0; i < c_retval; i++)
+            VIR_FREE(names[i]);
+        VIR_FREE(names);
+        return VIR_PY_NONE;
+    }
 
     if (names) {
         for (i = 0; i < c_retval; i++) {
@@ -4000,7 +4041,12 @@ libvirt_virConnectListSecrets(PyObject *self ATTRIBUTE_UNUSED,
             return VIR_PY_NONE;
         }
     }
-    py_retval = PyList_New(c_retval);
+    if (!(py_retval = PyList_New(c_retval))) {
+        for (i = 0; i < c_retval; i++)
+            VIR_FREE(uuids[i]);
+        VIR_FREE(uuids);
+        return VIR_PY_NONE;
+    }
 
     if (uuids) {
         for (i = 0; i < c_retval; i++) {
@@ -4219,7 +4265,12 @@ libvirt_virConnectListNWFilters(PyObject *self ATTRIBUTE_UNUSED,
             return VIR_PY_NONE;
         }
     }
-    py_retval = PyList_New(c_retval);
+    if (!(py_retval = PyList_New(c_retval))) {
+        for (i = 0; i < c_retval; i++)
+            VIR_FREE(uuids[i]);
+        VIR_FREE(uuids);
+        return VIR_PY_NONE;
+    }
 
     if (uuids) {
         for (i = 0; i < c_retval; i++) {
@@ -4502,7 +4553,8 @@ libvirt_virDomainGetJobInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
     LIBVIRT_END_ALLOW_THREADS;
     if (c_retval < 0)
         return VIR_PY_NONE;
-    py_retval = PyList_New(12);
+    if (!(py_retval = PyList_New(12)))
+        return VIR_PY_NONE;
     PyList_SET_ITEM(py_retval, 0, libvirt_intWrap((int) info.type));
     PyList_SET_ITEM(py_retval, 1, libvirt_ulonglongWrap(info.timeElapsed));
     PyList_SET_ITEM(py_retval, 2, libvirt_ulonglongWrap(info.timeRemaining));
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]