]> granicus.if.org Git - python/commitdiff
SF #1444030: Fix several potential defects found by Coverity.
authorHye-Shik Chang <hyeshik@gmail.com>
Tue, 7 Mar 2006 15:39:21 +0000 (15:39 +0000)
committerHye-Shik Chang <hyeshik@gmail.com>
Tue, 7 Mar 2006 15:39:21 +0000 (15:39 +0000)
(reviewed by Neal Norwitz)

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/ast.c
Python/ceval.c
Python/traceback.c

index be12775de3c4445d49e4555201b591e06fd71ffc..2318739a957e37cdd8e1fde695af7a5bd9060a1d 100644 (file)
@@ -1852,10 +1852,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 14e35f366e0c30da90028e623b327b38d54a72f8..fd28aa9324dee17cce77eb5612b653f9a7fc401d 100644 (file)
@@ -544,6 +544,7 @@ newOobject(int  size) {
        if (!self->buf) {
                   PyErr_SetString(PyExc_MemoryError,"out of memory");
                   self->buf_size = 0;
+                  Py_DECREF(self);
                   return NULL;
           }
 
index fa4380c0884fd303e0824a059984d9437f0f4338..637dc48aa5dc9eb524e9fc8a3dc12c9682d6dbdd 100644 (file)
@@ -1167,6 +1167,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 345d3b4b4fce0d0b9c1a1d4f323a7b4379303963..903265685fbc92476ed1eb0a933dab3c28615640 100644 (file)
@@ -2809,6 +2809,8 @@ long_bitwise(PyLongObject *a,
 
        if (a->ob_size < 0) {
                a = (PyLongObject *) long_invert(a);
+               if (a == NULL)
+                       return NULL;
                maska = MASK;
        }
        else {
@@ -2817,6 +2819,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 {
@@ -2868,7 +2874,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 0030ac7193f18e922e8e478fd4b8f512072b2516..e2b7603b5a233094bbd29679822744a4fdd76c98 100644 (file)
@@ -3276,7 +3276,7 @@ string_splitlines(PyStringObject *self, PyObject *args)
     return list;
 
  onError:
-    Py_DECREF(list);
+    Py_XDECREF(list);
     return NULL;
 }
 
index 3aaf98e841d9337e34290ef7334e2f75c1cf7e93..4146f1d880ea853eacb6eac04e72c976fd1a706b 100644 (file)
@@ -1876,16 +1876,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;
             }
@@ -1945,6 +1945,7 @@ ucnhashError:
         PyExc_UnicodeError,
         "\\N escapes not supported (can't load unicodedata module)"
         );
+    Py_XDECREF(v);
     Py_XDECREF(errorHandler);
     Py_XDECREF(exc);
     return NULL;
@@ -3962,7 +3963,7 @@ Py_ssize_t PyUnicode_Tailmatch(PyObject *str,
        return -1;
     substr = PyUnicode_FromObject(substr);
     if (substr == NULL) {
-       Py_DECREF(substr);
+       Py_DECREF(str);
        return -1;
     }
 
@@ -4429,7 +4430,7 @@ PyObject *PyUnicode_Splitlines(PyObject *string,
     return list;
 
  onError:
-    Py_DECREF(list);
+    Py_XDECREF(list);
     Py_DECREF(string);
     return NULL;
 }
@@ -6679,6 +6680,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;
@@ -6865,7 +6870,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;
            }
@@ -7163,6 +7168,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 4a64ef7cc250d4dffe58253810dc1219de0615ee..1d68bb5a7119e0f132c3cf3058b641e964ea6f1b 100644 (file)
@@ -903,8 +903,15 @@ PyObject_ClearWeakRefs(PyObject *object)
             }
         }
         else {
-            PyObject *tuple = PyTuple_New(count * 2);
+            PyObject *tuple;
             Py_ssize_t 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 5fbea395d85d989edc8ff60f4d6b02f7f186d2df..bb1774bb976d2f0310ef3a32adaec0fd65930ebd 100644 (file)
@@ -1438,6 +1438,8 @@ ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
             }
             /* extract Index values and put them in a Tuple */
             elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
+            if (!elts)
+                return NULL;
             for (j = 0; j < asdl_seq_LEN(slices); ++j) {
                 slc = (slice_ty)asdl_seq_GET(slices, j);
                 assert(slc->kind == Index_kind  && slc->v.Index.value);
index 5fda826e36812ad1081a3e0563ff50bdeeed12ec..b069c774249f97729b9903c5ea8a0f8cf50ed1a2 100644 (file)
@@ -3477,8 +3477,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 7b83d8b02d6603a155c232393e9de9cad6a3b83f..6c11cf527457f9aa9102468f92ececd2471bf88d 100644 (file)
@@ -185,8 +185,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 {