]> granicus.if.org Git - python/commitdiff
Issue #3299: Fix possible crash in the _sre module when given bad
authorAntoine Pitrou <solipsis@pitrou.net>
Thu, 14 Jan 2010 17:25:24 +0000 (17:25 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Thu, 14 Jan 2010 17:25:24 +0000 (17:25 +0000)
argument values in debug mode.  Patch by Victor Stinner.

Lib/test/test_re.py
Misc/NEWS
Modules/_sre.c

index 56056da85766cc1bcd5f6037c29ea8b30bc91aee..456917a43deba8bcba535444e6936ce912587130 100644 (file)
@@ -703,6 +703,12 @@ class ReTests(unittest.TestCase):
         self.assertEqual(pattern.sub('#', 'a\nb\nc'), 'a#\nb#\nc#')
         self.assertEqual(pattern.sub('#', '\n'), '#\n#')
 
+    def test_dealloc(self):
+        # issue 3299: check for segfault in debug build
+        import _sre
+        long_overflow = sys.maxsize + 2
+        self.assertRaises(TypeError, re.finditer, "a", {})
+        self.assertRaises(OverflowError, _sre.compile, "abc", 0, [long_overflow])
 
 def run_re_tests():
     from test.re_tests import benchmarks, tests, SUCCEED, FAIL, SYNTAX_ERROR
index 665c9241503488c9596efcc352e8e585c9f59737..2f90b5e1583b46c983257452a98dd7e7e434af57 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -32,6 +32,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #3299: Fix possible crash in the _sre module when given bad
+  argument values in debug mode.  Patch by Victor Stinner.
+
 - Issue #7703: Add support for the new buffer API to functions of the
   binascii module.  Backported from py3k by Florent Xicluna, with some
   additional tests.
index 0d9ee24eae7147013c69b0f445b990a6007e2b92..2cf97051eb1de2d5ceb8e60dee3c9215df252f69 100644 (file)
@@ -2684,6 +2684,10 @@ _compile(PyObject* self_, PyObject* args)
     self = PyObject_NEW_VAR(PatternObject, &Pattern_Type, n);
     if (!self)
         return NULL;
+    self->weakreflist = NULL;
+    self->pattern = NULL;
+    self->groupindex = NULL;
+    self->indexgroup = NULL;
 
     self->codesize = n;
 
@@ -2700,7 +2704,7 @@ _compile(PyObject* self_, PyObject* args)
     }
 
     if (PyErr_Occurred()) {
-        PyObject_DEL(self);
+        Py_DECREF(self);
         return NULL;
     }
 
@@ -3718,7 +3722,7 @@ static void
 scanner_dealloc(ScannerObject* self)
 {
     state_fini(&self->state);
-    Py_DECREF(self->pattern);
+    Py_XDECREF(self->pattern);
     PyObject_DEL(self);
 }
 
@@ -3840,10 +3844,11 @@ pattern_scanner(PatternObject* pattern, PyObject* args)
     self = PyObject_NEW(ScannerObject, &Scanner_Type);
     if (!self)
         return NULL;
+    self->pattern = NULL;
 
     string = state_init(&self->state, pattern, string, start, end);
     if (!string) {
-        PyObject_DEL(self);
+        Py_DECREF(self);
         return NULL;
     }