From 361cd4bd6c9c2d56ed154922e016c47d4bee9482 Mon Sep 17 00:00:00 2001 From: Hye-Shik Chang Date: Tue, 7 Mar 2006 15:59:09 +0000 Subject: Backport r42894: SF #1444030 Fix several potential defects found by Coverity. --- Modules/arraymodule.c | 11 +++++++---- Modules/cStringIO.c | 1 + Modules/zipimport.c | 2 ++ Objects/longobject.c | 8 +++++++- Objects/stringobject.c | 2 +- Objects/unicodeobject.c | 22 ++++++++++++++-------- Objects/weakrefobject.c | 9 ++++++++- Parser/firstsets.c | 2 ++ Python/ceval.c | 5 ++++- Python/traceback.c | 6 +++++- 10 files changed, 51 insertions(+), 17 deletions(-) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 0e78e65..f8c4918 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -1826,10 +1826,13 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) Py_DECREF(v); } } else if (initial != NULL && PyString_Check(initial)) { - PyObject *t_initial = PyTuple_Pack(1, - initial); - PyObject *v = - array_fromstring((arrayobject *)a, + PyObject *t_initial, *v; + t_initial = PyTuple_Pack(1, initial); + if (t_initial == NULL) { + Py_DECREF(a); + return NULL; + } + v = array_fromstring((arrayobject *)a, t_initial); Py_DECREF(t_initial); if (v == NULL) { diff --git a/Modules/cStringIO.c b/Modules/cStringIO.c index e891114..81b9ac8 100644 --- a/Modules/cStringIO.c +++ b/Modules/cStringIO.c @@ -541,6 +541,7 @@ newOobject(int size) { UNLESS (self->buf = (char *)malloc(size)) { PyErr_SetString(PyExc_MemoryError,"out of memory"); self->buf_size = 0; + Py_DECREF(self); return NULL; } diff --git a/Modules/zipimport.c b/Modules/zipimport.c index 3a902cd..2dc5048 100644 --- a/Modules/zipimport.c +++ b/Modules/zipimport.c @@ -1165,6 +1165,8 @@ initzipimport(void) mod = Py_InitModule4("zipimport", NULL, zipimport_doc, NULL, PYTHON_API_VERSION); + if (mod == NULL) + return; ZipImportError = PyErr_NewException("zipimport.ZipImportError", PyExc_ImportError, NULL); diff --git a/Objects/longobject.c b/Objects/longobject.c index ced72e1..0a354f1 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -2723,6 +2723,8 @@ long_bitwise(PyLongObject *a, if (a->ob_size < 0) { a = (PyLongObject *) long_invert(a); + if (a == NULL) + return NULL; maska = MASK; } else { @@ -2731,6 +2733,10 @@ long_bitwise(PyLongObject *a, } if (b->ob_size < 0) { b = (PyLongObject *) long_invert(b); + if (b == NULL) { + Py_DECREF(a); + return NULL; + } maskb = MASK; } else { @@ -2782,7 +2788,7 @@ long_bitwise(PyLongObject *a, : (maskb ? size_a : MIN(size_a, size_b))) : MAX(size_a, size_b); z = _PyLong_New(size_z); - if (a == NULL || b == NULL || z == NULL) { + if (z == NULL) { Py_XDECREF(a); Py_XDECREF(b); Py_XDECREF(z); diff --git a/Objects/stringobject.c b/Objects/stringobject.c index e7b6e69..7749167 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -3240,7 +3240,7 @@ string_splitlines(PyStringObject *self, PyObject *args) return list; onError: - Py_DECREF(list); + Py_XDECREF(list); return NULL; } diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 1104a66..310cdaf 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -1866,16 +1866,16 @@ PyObject *PyUnicode_DecodeUnicodeEscape(const char *s, message = "malformed \\N character escape"; if (ucnhash_CAPI == NULL) { /* load the unicode data module */ - PyObject *m, *v; + PyObject *m, *api; m = PyImport_ImportModule("unicodedata"); if (m == NULL) goto ucnhashError; - v = PyObject_GetAttrString(m, "ucnhash_CAPI"); + api = PyObject_GetAttrString(m, "ucnhash_CAPI"); Py_DECREF(m); - if (v == NULL) + if (api == NULL) goto ucnhashError; - ucnhash_CAPI = PyCObject_AsVoidPtr(v); - Py_DECREF(v); + ucnhash_CAPI = PyCObject_AsVoidPtr(api); + Py_DECREF(api); if (ucnhash_CAPI == NULL) goto ucnhashError; } @@ -1935,6 +1935,7 @@ ucnhashError: PyExc_UnicodeError, "\\N escapes not supported (can't load unicodedata module)" ); + Py_XDECREF(v); Py_XDECREF(errorHandler); Py_XDECREF(exc); return NULL; @@ -3911,7 +3912,7 @@ int PyUnicode_Tailmatch(PyObject *str, return -1; substr = PyUnicode_FromObject(substr); if (substr == NULL) { - Py_DECREF(substr); + Py_DECREF(str); return -1; } @@ -4382,7 +4383,7 @@ PyObject *PyUnicode_Splitlines(PyObject *string, return list; onError: - Py_DECREF(list); + Py_XDECREF(list); Py_DECREF(string); return NULL; } @@ -6627,6 +6628,10 @@ formatlong(PyObject *val, int flags, int prec, int type) if (!str) return NULL; result = _PyUnicode_New(len); + if (!result) { + Py_DECREF(str); + return NULL; + } for (i = 0; i < len; i++) result->str[i] = buf[i]; result->str[len] = 0; @@ -6813,7 +6818,7 @@ PyObject *PyUnicode_Format(PyObject *format, rescnt = fmtcnt + 100; reslen += rescnt; if (_PyUnicode_Resize(&result, reslen) < 0) - return NULL; + goto onError; res = PyUnicode_AS_UNICODE(result) + reslen - rescnt; --rescnt; } @@ -7111,6 +7116,7 @@ PyObject *PyUnicode_Format(PyObject *format, rescnt = width + fmtcnt + 100; reslen += rescnt; if (reslen < 0) { + Py_XDECREF(temp); Py_DECREF(result); return PyErr_NoMemory(); } diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index 5412dd3..bfc80d3 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -903,9 +903,16 @@ PyObject_ClearWeakRefs(PyObject *object) } } else { - PyObject *tuple = PyTuple_New(count * 2); + PyObject *tuple; int i = 0; + tuple = PyTuple_New(count * 2); + if (tuple == NULL) { + if (restore_error) + PyErr_Fetch(&err_type, &err_value, &err_tb); + return; + } + for (i = 0; i < count; ++i) { PyWeakReference *next = current->wr_next; diff --git a/Parser/firstsets.c b/Parser/firstsets.c index fcd9ffd..0f4e09d 100644 --- a/Parser/firstsets.c +++ b/Parser/firstsets.c @@ -107,4 +107,6 @@ calcfirstset(grammar *g, dfa *d) } printf(" }\n"); } + + PyMem_FREE(sym); } diff --git a/Python/ceval.c b/Python/ceval.c index 474d89b..d270c92 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -3407,8 +3407,11 @@ PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw) { PyObject *result; - if (arg == NULL) + if (arg == NULL) { arg = PyTuple_New(0); + if (arg == NULL) + return NULL; + } else if (!PyTuple_Check(arg)) { PyErr_SetString(PyExc_TypeError, "argument list must be a tuple"); diff --git a/Python/traceback.c b/Python/traceback.c index f40cfb4..cb568da 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -184,8 +184,12 @@ tb_displayline(PyObject *f, char *filename, int lineno, char *name) } PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name); err = PyFile_WriteString(linebuf, f); - if (xfp == NULL || err != 0) + if (xfp == NULL) return err; + else if (err != 0) { + fclose(xfp); + return err; + } for (i = 0; i < lineno; i++) { char* pLastChar = &linebuf[sizeof(linebuf)-2]; do { -- cgit v0.12