Check the allocation of b_objects and return if there was a failure.

Also fix a few memory leaks in other failure scenarios.

It seems that if b_objects == Py_None, we will have an extra ref to
b_objects.  Add XXX comment so hopefully someone documents why the
else isn't necessary or adds it in.

Reported by Klocwork #20
This commit is contained in:
Neal Norwitz 2006-07-23 07:55:55 +00:00
parent c09efa8444
commit 93f2ca1f85
1 changed files with 11 additions and 2 deletions

View File

@ -4521,20 +4521,29 @@ cast(void *ptr, PyObject *src, PyObject *ctype)
if (obj->b_objects == Py_None) { if (obj->b_objects == Py_None) {
Py_DECREF(Py_None); Py_DECREF(Py_None);
obj->b_objects = PyDict_New(); obj->b_objects = PyDict_New();
if (!obj->b_objects) {
Py_DECREF(result);
return NULL;
} }
}
/* XXX(nnorwitz): shouldn't the INCREF only be done in an else? */
Py_INCREF(obj->b_objects); Py_INCREF(obj->b_objects);
result->b_objects = obj->b_objects; result->b_objects = obj->b_objects;
if (result->b_objects) { if (result->b_objects) {
PyObject *index = PyLong_FromVoidPtr((void *)src); PyObject *index = PyLong_FromVoidPtr((void *)src);
int rc; int rc;
if (index == NULL) if (index == NULL) {
Py_DECREF(result);
return NULL; return NULL;
}
rc = PyDict_SetItem(result->b_objects, index, src); rc = PyDict_SetItem(result->b_objects, index, src);
Py_DECREF(index); Py_DECREF(index);
if (rc == -1) if (rc == -1) {
Py_DECREF(result);
return NULL; return NULL;
} }
} }
}
/* Should we assert that result is a pointer type? */ /* Should we assert that result is a pointer type? */
memcpy(result->b_ptr, &ptr, sizeof(void *)); memcpy(result->b_ptr, &ptr, sizeof(void *));
return (PyObject *)result; return (PyObject *)result;