diff options
author | Guannan Ren <gren@redhat.com> | 2012-09-27 01:33:50 +0800 |
---|---|---|
committer | Cole Robinson <crobinso@redhat.com> | 2012-10-18 12:59:01 -0400 |
commit | 10f2d97ecfbf8505120d1ebbd2c5717133c3d15f (patch) | |
tree | 099ae430ed3d4d764524950cd329daae859fe645 | |
parent | 48e9333bec0e96b667ba5a17093ac4aeef3491a3 (diff) | |
download | libvirt-python-10f2d97ecfbf8505120d1ebbd2c5717133c3d15f.tar.gz |
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
(cherry picked from commit 4c6be02a3e502027a819e20b6f5add8f63c20291)
-rw-r--r-- | typewrappers.c | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/typewrappers.c b/typewrappers.c index c525e59..7580689 100644 --- a/typewrappers.c +++ b/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; |