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

thomas.heller python-checkins at python.org
Thu Mar 16 20:24:31 CET 2006


Author: thomas.heller
Date: Thu Mar 16 20:24:27 2006
New Revision: 43083

Modified:
   python/trunk/Modules/_ctypes/callbacks.c
Log:
Rewrite the AllocFunctionCallback function for better error handling.
Hope that fixes one or two Coverty warnings.

Modified: python/trunk/Modules/_ctypes/callbacks.c
==============================================================================
--- python/trunk/Modules/_ctypes/callbacks.c	(original)
+++ python/trunk/Modules/_ctypes/callbacks.c	Thu Mar 16 20:24:27 2006
@@ -307,19 +307,18 @@
 
 	nArgs = PySequence_Size(converters);
 	p = (ffi_info *)PyMem_Malloc(sizeof(ffi_info) + sizeof(ffi_type) * (nArgs + 1));
-	if (p == NULL) {
-		PyErr_NoMemory();
-		return NULL;
-	}
+	if (p == NULL)
+		return (THUNK)PyErr_NoMemory();
 	p->pcl = MallocClosure();
 	if (p->pcl == NULL) {
-		PyMem_Free(p);
 		PyErr_NoMemory();
-		return NULL;
+		goto error;
 	}
 
 	for (i = 0; i < nArgs; ++i) {
 		PyObject *cnv = PySequence_GetItem(converters, i);
+		if (cnv == NULL)
+			goto error;
 		p->atypes[i] = GetType(cnv);
 		Py_DECREF(cnv);
 	}
@@ -330,10 +329,8 @@
 		p->restype = &ffi_type_void;
 	} else {
 		StgDictObject *dict = PyType_stgdict(restype);
-		if (dict == NULL) {
-			PyMem_Free(p);
-			return NULL;
-		}
+		if (dict == NULL)
+			goto error;
 		p->setfunc = dict->setfunc;
 		p->restype = &dict->ffi_type;
 	}
@@ -349,21 +346,25 @@
 	if (result != FFI_OK) {
 		PyErr_Format(PyExc_RuntimeError,
 			     "ffi_prep_cif failed with %d", result);
-		PyMem_Free(p);
-		return NULL;
+		goto error;
 	}
 	result = ffi_prep_closure(p->pcl, &p->cif, closure_fcn, p);
 	if (result != FFI_OK) {
 		PyErr_Format(PyExc_RuntimeError,
 			     "ffi_prep_closure failed with %d", result);
-		PyMem_Free(p);
-		return NULL;
+		goto error;
 	}
 
 	p->converters = converters;
 	p->callable = callable;
-
 	return (THUNK)p;
+
+  error:
+	if (p) {
+		FreeCallback((THUNK)p);
+		PyMem_Free(p);
+	}
+	return NULL;
 }
 
 /****************************************************************************


More information about the Python-checkins mailing list