]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Backport r42894: SF #1444030 Fix several potential defects found
authorHye-Shik Chang <hyeshik@gmail.com>
Tue, 7 Mar 2006 15:59:09 +0000 (15:59 +0000)
committerHye-Shik Chang <hyeshik@gmail.com>
Tue, 7 Mar 2006 15:59:09 +0000 (15:59 +0000)
by Coverity.

Modules/arraymodule.c
Modules/cStringIO.c
Modules/zipimport.c
Objects/longobject.c
Objects/stringobject.c
Objects/unicodeobject.c
Objects/weakrefobject.c
Parser/firstsets.c
Python/ceval.c
Python/traceback.c

index 0e78e650def2e66c64bdc3263dec6876df91f33a..f8c4918003e3e9818d473a74617fd3c5c4fde643 100644 (file)
@@ -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) {
index e89111415cf46139b23651972bac7ece890e8cad..81b9ac89e95b243f12ed9a316ae9aa1db5550a0a 100644 (file)
@@ -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;
           }
 
index 3a902cd72963e6c49512db79e12b42dbd14df041..2dc504837e8946d41f069b845f15664fbd88e51c 100644 (file)
@@ -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);
index ced72e108b3ef05f5e39829b4e6130eba8080045..0a354f1d854f21848f1b6d401cbfea315c8a0b7c 100644 (file)
@@ -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);
index e7b6e69b0da0b0eb379fa486bcbf14044f1bc582..7749167d3616c05b446c489b736d8d93c948bdab 100644 (file)
@@ -3240,7 +3240,7 @@ string_splitlines(PyStringObject *self, PyObject *args)
     return list;
 
  onError:
-    Py_DECREF(list);
+    Py_XDECREF(list);
     return NULL;
 }
 
index 1104a66487fe0b0f7fb3d0fe7f0168b784e10bc6..310cdafdd9b0fb1044e90e0bb3742161a2ea5da6 100644 (file)
@@ -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();
                }
index 5412dd3172610e84355cabf0dea193da61c4fa21..bfc80d387bcd787523c63c4b1a9b2f4b858716ae 100644 (file)
@@ -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;
 
index fcd9ffd240af62cbbaef6d966b042d98c2cce611..0f4e09d8180fb498d5a993d710b8ce3c58946e76 100644 (file)
@@ -107,4 +107,6 @@ calcfirstset(grammar *g, dfa *d)
                }
                printf(" }\n");
        }
+
+       PyMem_FREE(sym);
 }
index 474d89b1cd916e7f7d945796530fa873351b0160..d270c926e02c5531e8571a91a06fa8940073ed78 100644 (file)
@@ -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");
index f40cfb4a2365abbcb8dc1b71b981b50d03c6461c..cb568da2e199c16c628c227993499c0fa162e448 100644 (file)
@@ -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 {