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

[libvirt] [PATCH 2/2] python: return error if PyObject obj is NULL for unwrapper helper functions



The result is indeterminate for NULL argument to python
functions as follows. It's better to return negative value in
these situations.

PyObject_IsTrue will segfault if the argument is NULL
PyFloat_AsDouble(NULL) is -1.000000
PyLong_AsUnsignedLongLong(NULL) is 0.000000
---
 python/typewrappers.c | 43 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)

diff --git a/python/typewrappers.c b/python/typewrappers.c
index c525e59..7580689 100644
--- a/python/typewrappers.c
+++ b/python/typewrappers.c
@@ -124,6 +124,11 @@ libvirt_intUnwrap(PyObject *obj, int *val)
 {
     long long_val;
 
+    if (!obj) {
+        PyErr_SetString(PyExc_TypeError, "unexpected type");
+        return -1;
+    }
+
     /* If obj is type of PyInt_Type, PyInt_AsLong converts it
      * to C long type directly. If it is of PyLong_Type, PyInt_AsLong
      * will call PyLong_AsLong() to deal with it automatically.
@@ -151,6 +156,11 @@ libvirt_uintUnwrap(PyObject *obj, unsigned int *val)
 {
     long long_val;
 
+    if (!obj) {
+        PyErr_SetString(PyExc_TypeError, "unexpected type");
+        return -1;
+    }
+
     long_val = PyInt_AsLong(obj);
     if ((long_val == -1) && PyErr_Occurred())
         return -1;
@@ -170,6 +180,11 @@ libvirt_longUnwrap(PyObject *obj, long *val)
 {
     long long_val;
 
+    if (!obj) {
+        PyErr_SetString(PyExc_TypeError, "unexpected type");
+        return -1;
+    }
+
     long_val = PyInt_AsLong(obj);
     if ((long_val == -1) && PyErr_Occurred())
         return -1;
@@ -183,6 +198,11 @@ libvirt_ulongUnwrap(PyObject *obj, unsigned long *val)
 {
     long long_val;
 
+    if (!obj) {
+        PyErr_SetString(PyExc_TypeError, "unexpected type");
+        return -1;
+    }
+
     long_val = PyInt_AsLong(obj);
     if ((long_val == -1) && PyErr_Occurred())
         return -1;
@@ -202,6 +222,11 @@ libvirt_longlongUnwrap(PyObject *obj, long long *val)
 {
     long long llong_val;
 
+    if (!obj) {
+        PyErr_SetString(PyExc_TypeError, "unexpected type");
+        return -1;
+    }
+
     /* If obj is of PyInt_Type, PyLong_AsLongLong
      * will call PyInt_AsLong() to handle it automatically.
      */
@@ -219,6 +244,11 @@ libvirt_ulonglongUnwrap(PyObject *obj, unsigned long long *val)
     unsigned long long ullong_val = -1;
     long long llong_val;
 
+    if (!obj) {
+        PyErr_SetString(PyExc_TypeError, "unexpected type");
+        return -1;
+    }
+
     /* The PyLong_AsUnsignedLongLong doesn't check the type of
      * obj, only accept argument of PyLong_Type, so we check it instead.
      */
@@ -247,6 +277,11 @@ libvirt_doubleUnwrap(PyObject *obj, double *val)
 {
     double double_val;
 
+    if (!obj) {
+        PyErr_SetString(PyExc_TypeError, "unexpected type");
+        return -1;
+    }
+
     double_val = PyFloat_AsDouble(obj);
     if ((double_val == -1) && PyErr_Occurred())
         return -1;
@@ -260,8 +295,12 @@ libvirt_boolUnwrap(PyObject *obj, bool *val)
 {
     int ret;
 
-    ret = PyObject_IsTrue(obj);
-    if (ret < 0)
+    if (!obj) {
+        PyErr_SetString(PyExc_TypeError, "unexpected type");
+        return -1;
+    }
+
+    if ((ret = PyObject_IsTrue(obj)) < 0)
         return ret;
 
     *val = ret > 0;
-- 
1.7.11.2


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