]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[Backport r52147 | andrew.kuchling]
authorAndrew M. Kuchling <amk@amk.ca>
Thu, 5 Oct 2006 17:30:48 +0000 (17:30 +0000)
committerAndrew M. Kuchling <amk@amk.ca>
Thu, 5 Oct 2006 17:30:48 +0000 (17:30 +0000)
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
Modules/_sre.c

index d68e31ff354b800e4ef1b33341056f273a5de75a..d6eee1c4fdd83f7184ed863a4548e9ebc9e00b36 100644 (file)
--- 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.
 
index a1c596a0d63cebc213a40c3e8e00d99684d69692..200c80ef4a0438d2fc94a13d24c5c8d5a3555622 100644 (file)
@@ -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);