[Python-checkins] r50780 - python/trunk/Modules/_ctypes/_ctypes.c

neal.norwitz python-checkins at python.org
Sun Jul 23 09:55:55 CEST 2006


Author: neal.norwitz
Date: Sun Jul 23 09:55:55 2006
New Revision: 50780

Modified:
   python/trunk/Modules/_ctypes/_ctypes.c
Log:
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


Modified: python/trunk/Modules/_ctypes/_ctypes.c
==============================================================================
--- python/trunk/Modules/_ctypes/_ctypes.c	(original)
+++ python/trunk/Modules/_ctypes/_ctypes.c	Sun Jul 23 09:55:55 2006
@@ -4521,18 +4521,27 @@
 		if (obj->b_objects == Py_None) {
 			Py_DECREF(Py_None);
 			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);
 		result->b_objects = obj->b_objects;
 		if (result->b_objects) {
 			PyObject *index = PyLong_FromVoidPtr((void *)src);
 			int rc;
-			if (index == NULL)
+			if (index == NULL) {
+				Py_DECREF(result);
 				return NULL;
+			}
 			rc = PyDict_SetItem(result->b_objects, index, src);
 			Py_DECREF(index);
-			if (rc == -1)
+			if (rc == -1) {
+				Py_DECREF(result);
 				return NULL;
+			}
 		}
 	}
 	/* Should we assert that result is a pointer type? */


More information about the Python-checkins mailing list