From 4eb4416e7b5bb43e1194629e4895dc1d087dc0ab Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 13 Jan 2010 19:10:19 +0100 Subject: [PATCH 1/2] issue 3299: replace PyObject_DEL() by Py_DECREF() in _sre module PyObject_DEL() should only be used in a destructor to free memory, it doesn't update the global reference counters. --- Lib/test/test_re.py | 6 ++++++ Modules/_sre.c | 11 ++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index 56056da..a7f176a 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -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(TypeError, _sre.compile, "abc", 0, [long_overflow]) def run_re_tests(): from test.re_tests import benchmarks, tests, SUCCEED, FAIL, SYNTAX_ERROR diff --git a/Modules/_sre.c b/Modules/_sre.c index 0d9ee24..2cf9705 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -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; } -- 1.6.6