From 93bf902242ebd991e11bd4421847f6eaafd5c8d4 Mon Sep 17 00:00:00 2001 From: Neal Norwitz Date: Sun, 13 Aug 2006 18:40:39 +0000 Subject: [PATCH] Handle alloca failures. Klocwork 225-228 --- Modules/_ctypes/_ctypes.c | 2 ++ Modules/_ctypes/callproc.c | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index d26ad1d3e26..cf0b20dd6f9 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -2475,6 +2475,8 @@ static PPROC FindAddress(void *handle, char *name, PyObject *type) where n is 0, 4, 8, 12, ..., 128 */ mangled_name = alloca(strlen(name) + 1 + 1 + 1 + 3); /* \0 _ @ %d */ + if (!mangled_name) + return NULL; for (i = 0; i < 32; ++i) { sprintf(mangled_name, "_%s@%d", name, i*4); address = (PPROC)GetProcAddress(handle, mangled_name); diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c index d6f875b9460..77f879eab80 100644 --- a/Modules/_ctypes/callproc.c +++ b/Modules/_ctypes/callproc.c @@ -915,6 +915,10 @@ PyObject *_CallProc(PPROC pProc, #endif args = (struct argument *)alloca(sizeof(struct argument) * argcount); + if (!args) { + PyErr_NoMemory(); + return NULL; + } memset(args, 0, sizeof(struct argument) * argcount); argtype_count = argtypes ? PyTuple_GET_SIZE(argtypes) : 0; #ifdef MS_WIN32 @@ -968,6 +972,10 @@ PyObject *_CallProc(PPROC pProc, avalues = (void **)alloca(sizeof(void *) * argcount); atypes = (ffi_type **)alloca(sizeof(ffi_type *) * argcount); + if (!resbuf || !avalues || !atypes) { + PyErr_NoMemory(); + goto cleanup; + } for (i = 0; i < argcount; ++i) { atypes[i] = args[i].ffi_type; if (atypes[i]->type == FFI_TYPE_STRUCT) @@ -1068,6 +1076,11 @@ static PyObject *load_library(PyObject *self, PyObject *args) return NULL; #ifdef _UNICODE name = alloca((PyString_Size(nameobj) + 1) * sizeof(WCHAR)); + if (!name) { + PyErr_NoMemory(); + return NULL; + } + { int r; char *aname = PyString_AsString(nameobj);