From 9781e5d97e4648453b6b4d011b7252c1cb05656c Mon Sep 17 00:00:00 2001 From: "Andrew M. Kuchling" Date: Thu, 5 Oct 2006 18:22:02 +0000 Subject: [Partial backport of r50773 | neal.norwitz -- other parts of this patch 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 | 28 ++++++++++++++++++++-------- Modules/cPickle.c | 13 +++++++++---- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/Modules/bz2module.c b/Modules/bz2module.c index 272b05f..9772558 100644 --- a/Modules/bz2module.c +++ b/Modules/bz2module.c @@ -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; } diff --git a/Modules/cPickle.c b/Modules/cPickle.c index df564d4..086f1a7 100644 --- a/Modules/cPickle.c +++ b/Modules/cPickle.c @@ -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; -- cgit v0.12