diff options
author | Thomas Heller <theller@ctypes.org> | 2006-03-16 19:24:27 (GMT) |
---|---|---|
committer | Thomas Heller <theller@ctypes.org> | 2006-03-16 19:24:27 (GMT) |
commit | 127551637bc65d0e268aad10d65c48147ae566e8 (patch) | |
tree | d7011bfab8c29dc01a1bcb9525b9ca33dac20a38 /Modules | |
parent | d71afb2d105a083babb54511f1e85b6aea6d3c38 (diff) | |
download | cpython-127551637bc65d0e268aad10d65c48147ae566e8.zip cpython-127551637bc65d0e268aad10d65c48147ae566e8.tar.gz cpython-127551637bc65d0e268aad10d65c48147ae566e8.tar.bz2 |
Rewrite the AllocFunctionCallback function for better error handling.
Hope that fixes one or two Coverty warnings.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_ctypes/callbacks.c | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c index 2948d98..286faa3 100644 --- a/Modules/_ctypes/callbacks.c +++ b/Modules/_ctypes/callbacks.c @@ -307,19 +307,18 @@ THUNK AllocFunctionCallback(PyObject *callable, 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 @@ THUNK AllocFunctionCallback(PyObject *callable, 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 @@ THUNK AllocFunctionCallback(PyObject *callable, 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; } /**************************************************************************** |