summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuannan Ren <gren@redhat.com>2012-09-27 01:33:50 +0800
committerGuannan Ren <gren@redhat.com>2012-09-28 16:43:12 +0800
commitb9355e94901e925dd2ac23935f03ef2385cc262a (patch)
tree099ae430ed3d4d764524950cd329daae859fe645
parent48e9333bec0e96b667ba5a17093ac4aeef3491a3 (diff)
downloadlibvirt-python-split-b9355e94901e925dd2ac23935f03ef2385cc262a.zip
libvirt-python-split-b9355e94901e925dd2ac23935f03ef2385cc262a.tar.gz
libvirt-python-split-b9355e94901e925dd2ac23935f03ef2385cc262a.tar.xz
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
-rw-r--r--typewrappers.c43
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;