]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[Partial backport of r50773 | neal.norwitz -- other parts of this patch
authorAndrew M. Kuchling <amk@amk.ca>
Thu, 5 Oct 2006 18:22:02 +0000 (18:22 +0000)
committerAndrew M. Kuchling <amk@amk.ca>
Thu, 5 Oct 2006 18:22:02 +0000 (18:22 +0000)
 applied to pyarena.c, compile.c, and symtable.c, which were different in 2.4.]

Fix more memory allocation issues found with failmalloc.

Modules/bz2module.c
Modules/cPickle.c

index 272b05f9b36df957637cadec8b14336a6d9e8d3c..97725582b67528cec0d03492d52ba6585c12e01d 100644 (file)
@@ -1359,8 +1359,10 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs)
 
 #ifdef WITH_THREAD
        self->lock = PyThread_allocate_lock();
-       if (!self->lock)
+       if (!self->lock) {
+               PyErr_SetString(PyExc_MemoryError, "unable to allocate lock");
                goto error;
+       }
 #endif
 
        if (mode_char == 'r')
@@ -1382,10 +1384,12 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs)
        return 0;
 
 error:
-       Py_DECREF(self->file);
+       Py_CLEAR(self->file);
 #ifdef WITH_THREAD
-       if (self->lock)
+       if (self->lock) {
                PyThread_free_lock(self->lock);
+               self->lock = NULL;
+       }
 #endif
        return -1;
 }
@@ -1693,8 +1697,10 @@ BZ2Comp_init(BZ2CompObject *self, PyObject *args, PyObject *kwargs)
 
 #ifdef WITH_THREAD
        self->lock = PyThread_allocate_lock();
-       if (!self->lock)
+       if (!self->lock) {
+               PyErr_SetString(PyExc_MemoryError, "unable to allocate lock");
                goto error;
+       }
 #endif
 
        memset(&self->bzs, 0, sizeof(bz_stream));
@@ -1709,8 +1715,10 @@ BZ2Comp_init(BZ2CompObject *self, PyObject *args, PyObject *kwargs)
        return 0;
 error:
 #ifdef WITH_THREAD
-       if (self->lock)
+       if (self->lock) {
                PyThread_free_lock(self->lock);
+               self->lock = NULL;
+       }
 #endif
        return -1;
 }
@@ -1905,8 +1913,10 @@ BZ2Decomp_init(BZ2DecompObject *self, PyObject *args, PyObject *kwargs)
 
 #ifdef WITH_THREAD
        self->lock = PyThread_allocate_lock();
-       if (!self->lock)
+       if (!self->lock) {
+               PyErr_SetString(PyExc_MemoryError, "unable to allocate lock");
                goto error;
+       }
 #endif
 
        self->unused_data = PyString_FromString("");
@@ -1926,10 +1936,12 @@ BZ2Decomp_init(BZ2DecompObject *self, PyObject *args, PyObject *kwargs)
 
 error:
 #ifdef WITH_THREAD
-       if (self->lock)
+       if (self->lock) {
                PyThread_free_lock(self->lock);
+               self->lock = NULL;
+       }
 #endif
-       Py_XDECREF(self->unused_data);
+       Py_CLEAR(self->unused_data);
        return -1;
 }
 
index df564d44b3b59859e910c0c2c38563ae7fda36ee..086f1a7dbb8eaeb2be907469026cff0497267ff4 100644 (file)
@@ -196,7 +196,7 @@ Pdata_clear(Pdata *self, int clearto)
        for (i = self->length, p = self->data + clearto;
             --i >= clearto;
             p++) {
-               Py_DECREF(*p);
+               Py_CLEAR(*p);
        }
        self->length = clearto;
 
@@ -208,6 +208,7 @@ Pdata_grow(Pdata *self)
 {
        int bigger;
        size_t nbytes;
+       PyObject **tmp;
 
        bigger = self->size << 1;
        if (bigger <= 0)        /* was 0, or new value overflows */
@@ -217,14 +218,14 @@ Pdata_grow(Pdata *self)
        nbytes = (size_t)bigger * sizeof(PyObject *);
        if (nbytes / sizeof(PyObject *) != (size_t)bigger)
                goto nomemory;
-       self->data = realloc(self->data, nbytes);
-       if (self->data == NULL)
+       tmp = realloc(self->data, nbytes);
+       if (tmp == NULL)
                goto nomemory;
+       self->data = tmp;
        self->size = bigger;
        return 0;
 
   nomemory:
-       self->size = 0;
        PyErr_NoMemory();
        return -1;
 }
@@ -4168,6 +4169,7 @@ do_append(Unpicklerobject *self, int  x)
                int list_len;
 
                slice=Pdata_popList(self->stack, x);
+               if (!slice) return -1;
                list_len = PyList_GET_SIZE(list);
                i=PyList_SetSlice(list, list_len, list_len, slice);
                Py_DECREF(slice);
@@ -5178,6 +5180,9 @@ newUnpicklerobject(PyObject *f)
        if (!( self->memo = PyDict_New()))
                goto err;
 
+       if (!self->stack)
+               goto err;
+
        Py_INCREF(f);
        self->file = f;