From 82b230732fab6dc3b8d945feb38bd99712a501fc Mon Sep 17 00:00:00 2001 From: Fredrik Lundh Date: Sun, 9 Dec 2001 16:13:15 +0000 Subject: bug #133283, #477728, #483789, #490573 backed out of broken minimal repeat patch from July also fixed a couple of minor potential resource leaks in pattern_subx (Guido had already fixed the big one) --- Lib/sre_parse.py | 4 ++-- Lib/test/re_tests.py | 10 ++++++++-- Lib/test/test_sre.py | 2 +- Modules/_sre.c | 30 +++++++++++------------------- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py index 7313a1f..f6b53de 100644 --- a/Lib/sre_parse.py +++ b/Lib/sre_parse.py @@ -83,8 +83,8 @@ class Pattern: if name: ogid = self.groupdict.get(name, None) if ogid is not None: - raise error, ("redefinition of group name %s as group %d; " + - "was group %d") % (`name`, gid, ogid) + raise error, ("redefinition of group name %s as group %d; " + "was group %d" % (repr(name), gid, ogid)) self.groupdict[name] = gid self.open.append(gid) return gid diff --git a/Lib/test/re_tests.py b/Lib/test/re_tests.py index d69b324..953e4fd 100755 --- a/Lib/test/re_tests.py +++ b/Lib/test/re_tests.py @@ -638,8 +638,14 @@ xyzabc (r'(?i)[m]+', 'MMM', SUCCEED, 'found', 'MMM'), # bug 130748: ^* should be an error (nothing to repeat) (r'^*', '', SYNTAX_ERROR), - # bug 133283: minimizing repeat bug - (r'"(?:\\"|[^"])*?"', r'"\""', SUCCEED, 'found', r'"\"'), + # bug 133283: minimizing repeat problem + (r'"(?:\\"|[^"])*?"', r'"\""', SUCCEED, 'found', r'"\""'), + # bug 477728: minimizing repeat problem + (r'^.*?$', 'one\ntwo\nthree\n', FAIL), + # bug 483789: minimizing repeat problem + (r'a[^>]*?b', 'a>b', FAIL), + # bug 490573: minimizing repeat problem + (r'^a*?$', 'foo', FAIL), ] try: diff --git a/Lib/test/test_sre.py b/Lib/test/test_sre.py index 6442036..abd55db 100644 --- a/Lib/test/test_sre.py +++ b/Lib/test/test_sre.py @@ -296,7 +296,7 @@ if verbose: # implementation of repeated groups. test("sre.match('(x)*', 50000*'x').span()", (0, 50000), RuntimeError) test("sre.match(r'(x)*y', 50000*'x'+'y').span()", (0, 50001), RuntimeError) -test("sre.match(r'(x)*?y', 50000*'x'+'y').span()", (0, 50001)) +test("sre.match(r'(x)*?y', 50000*'x'+'y').span()", (0, 50001), RuntimeError) from re_tests import * diff --git a/Modules/_sre.c b/Modules/_sre.c index b62b17f..769965f 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -36,6 +36,7 @@ * 2001-10-21 fl added sub/subn primitive * 2001-10-22 fl check for literal sub/subn templates * 2001-10-24 fl added finditer primitive (for 2.2 only) + * 2001-12-07 fl fixed memory leak in sub/subn (Guido van Rossum) * * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved. * @@ -1116,21 +1117,9 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level) /* see if the tail matches */ state->repeat = rp->prev; - /* FIXME: the following fix doesn't always work (#133283) */ - if (rp->pattern[2] == 65535) { - /* unbounded repeat */ - for (;;) { - i = SRE_MATCH(state, pattern, level + 1); - if (i || ptr >= end) - break; - state->ptr = ++ptr; - } - } else - i = SRE_MATCH(state, pattern, level + 1); - if (i) { - /* free(rp); */ + i = SRE_MATCH(state, pattern, level + 1); + if (i) return i; - } state->ptr = ptr; state->repeat = rp; @@ -2143,11 +2132,14 @@ pattern_subx(PatternObject* self, PyObject* template, PyObject* string, } string = state_init(&state, self, string, 0, INT_MAX); - if (!string) + if (!string) { + Py_DECREF(filter); return NULL; + } list = PyList_New(0); if (!list) { + Py_DECREF(filter); state_fini(&state); return NULL; } @@ -2260,9 +2252,9 @@ next: return item; error: - Py_DECREF(filter); Py_DECREF(list); state_fini(&state); + Py_DECREF(filter); return NULL; } @@ -2403,7 +2395,7 @@ pattern_getattr(PatternObject* self, char* name) statichere PyTypeObject Pattern_Type = { PyObject_HEAD_INIT(NULL) - 0, "_sre.SRE_Pattern", + 0, "_" SRE_MODULE ".SRE_Pattern", sizeof(PatternObject), sizeof(SRE_CODE), (destructor)pattern_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ @@ -2866,7 +2858,7 @@ match_getattr(MatchObject* self, char* name) statichere PyTypeObject Match_Type = { PyObject_HEAD_INIT(NULL) - 0, "_sre.SRE_Match", + 0, "_" SRE_MODULE ".SRE_Match", sizeof(MatchObject), sizeof(int), (destructor)match_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ @@ -2974,7 +2966,7 @@ scanner_getattr(ScannerObject* self, char* name) statichere PyTypeObject Scanner_Type = { PyObject_HEAD_INIT(NULL) - 0, "_sre.SRE_Scanner", + 0, "_" SRE_MODULE ".SRE_Scanner", sizeof(ScannerObject), 0, (destructor)scanner_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ -- cgit v0.12