From: Georg Brandl Date: Fri, 17 Mar 2006 19:04:15 +0000 (+0000) Subject: Backport: Fix missing NULL checks after PyTuple_New, PyList_New, PyDict_New X-Git-Tag: v2.4.3c1~18 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d9ca66f2b2073529c335cc953d202f4068a7a870;p=thirdparty%2FPython%2Fcpython.git Backport: Fix missing NULL checks after PyTuple_New, PyList_New, PyDict_New --- diff --git a/Modules/almodule.c b/Modules/almodule.c index 12b265e90ece..e457f86baf9f 100644 --- a/Modules/almodule.c +++ b/Modules/almodule.c @@ -1482,7 +1482,8 @@ al_GetParams(PyObject *self, PyObject *args) } if (alGetParams(resource, pvs, npvs) < 0) goto error; - v = PyList_New(npvs); + if (!(v = PyList_New(npvs))) + goto error; for (i = 0; i < npvs; i++) { if (pvs[i].sizeOut < 0) { char buf[32]; @@ -1692,6 +1693,7 @@ al_GetParamInfo(PyObject *self, PyObject *args) if (alGetParamInfo(res, param, &pinfo) < 0) return NULL; v = PyDict_New(); + if (!v) return NULL; item = PyInt_FromLong((long) pinfo.resource); PyDict_SetItemString(v, "resource", item); diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c index c563ed84d246..5366209c9a17 100644 --- a/Modules/gcmodule.c +++ b/Modules/gcmodule.c @@ -1047,6 +1047,7 @@ gc_get_referrers(PyObject *self, PyObject *args) { int i; PyObject *result = PyList_New(0); + if (!result) return NULL; for (i = 0; i < NUM_GENERATIONS; i++) { if (!(gc_referrers_for(args, GEN_HEAD(i), result))) { Py_DECREF(result); diff --git a/Modules/ossaudiodev.c b/Modules/ossaudiodev.c index 047355fb784d..11592353ce52 100644 --- a/Modules/ossaudiodev.c +++ b/Modules/ossaudiodev.c @@ -908,24 +908,32 @@ build_namelists (PyObject *module) labels = PyList_New(num_controls); names = PyList_New(num_controls); + if (labels == NULL || names == NULL) + goto error2; for (i = 0; i < num_controls; i++) { s = PyString_FromString(control_labels[i]); if (s == NULL) - return -1; + goto error2; PyList_SET_ITEM(labels, i, s); s = PyString_FromString(control_names[i]); if (s == NULL) - return -1; + goto error2; PyList_SET_ITEM(names, i, s); } if (PyModule_AddObject(module, "control_labels", labels) == -1) - return -1; + goto error2; if (PyModule_AddObject(module, "control_names", names) == -1) - return -1; + goto error1; return 0; + +error2: + Py_XDECREF(labels); +error1: + Py_XDECREF(names); + return -1; } diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index eb6b22f1e05e..2317d2ec0132 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -1506,7 +1506,7 @@ xmlparse_getattr(xmlparseobject *self, char *name) return self->intern; } } - + #define APPEND(list, str) \ do { \ PyObject *o = PyString_FromString(str); \ @@ -1518,6 +1518,8 @@ xmlparse_getattr(xmlparseobject *self, char *name) if (strcmp(name, "__members__") == 0) { int i; PyObject *rc = PyList_New(0); + if (!rc) + return NULL; for (i = 0; handler_info[i].name != NULL; i++) { PyObject *o = get_handler_name(&handler_info[i]); if (o != NULL) diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index ea7d75302238..74b905626c17 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -615,6 +615,7 @@ tuplesubscript(PyTupleObject* self, PyObject* item) } else { result = PyTuple_New(slicelength); + if (!result) return NULL; src = self->ob_item; dest = ((PyTupleObject *)result)->ob_item; diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 5fec015f78b5..bb4b7a8cf020 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -1105,14 +1105,17 @@ set_mro_error(PyObject *to_merge, int *remain) char buf[1000]; PyObject *k, *v; PyObject *set = PyDict_New(); + if (!set) return; to_merge_size = PyList_GET_SIZE(to_merge); for (i = 0; i < to_merge_size; i++) { PyObject *L = PyList_GET_ITEM(to_merge, i); if (remain[i] < PyList_GET_SIZE(L)) { PyObject *c = PyList_GET_ITEM(L, remain[i]); - if (PyDict_SetItem(set, c, Py_None) < 0) + if (PyDict_SetItem(set, c, Py_None) < 0) { + Py_DECREF(set); return; + } } } n = PyDict_Size(set);