Skip to content

Commit 541de35

Browse files
gh-67877: Fix memory leaks in terminated RE matching
If SRE(match) function terminates abruptly, either because of a signal or because memory allocation fails, allocated SRE_REPEAT blocks might be never released.
1 parent 7577307 commit 541de35

File tree

6 files changed

+222
-36
lines changed

6 files changed

+222
-36
lines changed

Lib/test/test_re.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2681,6 +2681,27 @@ def test_character_set_none(self):
26812681
self.assertIsNone(re.search(p, s))
26822682
self.assertIsNone(re.search('(?s:.)' + p, s))
26832683

2684+
def check_interrupt(self, pattern, string, maxcount):
2685+
class Interrupt(Exception):
2686+
pass
2687+
p = re.compile(pattern)
2688+
for n in range(maxcount):
2689+
p._fail_after(n, Interrupt)
2690+
try:
2691+
p.match(string)
2692+
return n
2693+
except Interrupt:
2694+
pass
2695+
2696+
@unittest.skipUnless(hasattr(re.Pattern, '_fail_after'), 'requires debug build')
2697+
def test_memory_leaks(self):
2698+
self.check_interrupt(r'(.)*:', 'abc:', 100)
2699+
self.check_interrupt(r'([^:])*?:', 'abc:', 100)
2700+
self.check_interrupt(r'([^:])*+:', 'abc:', 100)
2701+
self.check_interrupt(r'(.){2,4}:', 'abc:', 100)
2702+
self.check_interrupt(r'([^:]){2,4}?:', 'abc:', 100)
2703+
self.check_interrupt(r'([^:]){2,4}+:', 'abc:', 100)
2704+
26842705

26852706
def get_debug_out(pat):
26862707
with captured_stdout() as out:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix memory leaks when :mod:`regular expression <re>` matching terminates
2+
abruptly, either because of a signal or because memory allocation fails.

Modules/_sre/clinic/sre.c.h

Lines changed: 43 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)