From af4defb79ddec0c5db161f9b084097b843918737 Mon Sep 17 00:00:00 2001 From: "Andrew M. Kuchling" Date: Thu, 5 Oct 2006 17:30:48 +0000 Subject: [Backport r52147 | andrew.kuchling] Cause a PyObject_Malloc() failure to trigger a MemoryError, and then add 'if (PyErr_Occurred())' checks to various places so that NULL is returned properly. --- Misc/NEWS | 2 ++ Modules/_sre.c | 23 +++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS b/Misc/NEWS index d68e31f..d6eee1c 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -91,6 +91,8 @@ Extension Modules - Patch #1535500: fix segfault in BZ2File.writelines and make sure it raises the correct exceptions. +- Make regex engine raise MemoryError if allocating memory fails. + - Bug #1471938: Fix curses module build problem on Solaris 8; patch by Paul Eggert. diff --git a/Modules/_sre.c b/Modules/_sre.c index a1c596a..200c80e 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -1162,9 +1162,10 @@ entrance: /* install new repeat context */ ctx->u.rep = (SRE_REPEAT*) malloc(sizeof(*ctx->u.rep)); - /* XXX(nnorwitz): anything else we need to do on error? */ - if (!ctx->u.rep) + if (!ctx->u.rep) { + PyErr_NoMemory(); RETURN_FAILURE; + } ctx->u.rep->count = -1; ctx->u.rep->pattern = ctx->pattern; ctx->u.rep->prev = state->repeat; @@ -2032,6 +2033,8 @@ pattern_match(PatternObject* self, PyObject* args, PyObject* kw) } TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr)); + if (PyErr_Occurred()) + return NULL; state_fini(&state); @@ -2070,6 +2073,9 @@ pattern_search(PatternObject* self, PyObject* args, PyObject* kw) state_fini(&state); + if (PyErr_Occurred()) + return NULL; + return pattern_new_match(self, &state, status); } @@ -2219,6 +2225,9 @@ pattern_findall(PatternObject* self, PyObject* args, PyObject* kw) #endif } + if (PyErr_Occurred()) + goto error; + if (status <= 0) { if (status == 0) break; @@ -2346,6 +2355,9 @@ pattern_split(PatternObject* self, PyObject* args, PyObject* kw) #endif } + if (PyErr_Occurred()) + goto error; + if (status <= 0) { if (status == 0) break; @@ -2493,6 +2505,9 @@ pattern_subx(PatternObject* self, PyObject* template, PyObject* string, #endif } + if (PyErr_Occurred()) + goto error; + if (status <= 0) { if (status == 0) break; @@ -3289,6 +3304,8 @@ scanner_match(ScannerObject* self, PyObject* args) status = sre_umatch(state, PatternObject_GetCode(self->pattern)); #endif } + if (PyErr_Occurred()) + return NULL; match = pattern_new_match((PatternObject*) self->pattern, state, status); @@ -3320,6 +3337,8 @@ scanner_search(ScannerObject* self, PyObject* args) status = sre_usearch(state, PatternObject_GetCode(self->pattern)); #endif } + if (PyErr_Occurred()) + return NULL; match = pattern_new_match((PatternObject*) self->pattern, state, status); -- cgit v0.12