]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
python: return error if PyObject obj is NULL for unwrapper helper functions
authorGuannan Ren <gren@redhat.com>
Wed, 26 Sep 2012 17:33:50 +0000 (01:33 +0800)
committerGuannan Ren <gren@redhat.com>
Fri, 28 Sep 2012 08:43:12 +0000 (16:43 +0800)
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

index c525e592a3cce0d54cc004f5817d2ed182aa05af..75806897b9f22952d82b8f13e65b179a917c983a 100644 (file)
@@ -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;