Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-23689: re module, fix memory leak when a match is terminated by a signal or memory allocation failure #32283

Merged
merged 11 commits into from Apr 3, 2022
6 changes: 3 additions & 3 deletions Lib/test/test_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -2338,8 +2338,8 @@ def test_possesive_repeat(self):
''')

def test_repeat_index(self):
self.assertEqual(get_debug_out(r'(?:ab)*(?:cd)*'), '''\
MAX_REPEAT 0 MAXREPEAT
self.assertEqual(get_debug_out(r'(?:ab)*?(?:cd)*'), '''\
MIN_REPEAT 0 MAXREPEAT
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You just read my mind! I was going to propose such a change, but I thought that I was already bothering you too much.

Copy link
Author

@ghost ghost Apr 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just thought this after posting this PR.

I thought that I was already bothering you too much.

As an inactive contributor, this is not a matter.
I'm not practised, so need continuously improve the patch to get to a good state.
When I think it's good, I can always find its shortcomings afterwards.

I have only one question: how to prove that we need only one SRE_REPEAT structure per the REPEAT code?

I have to think about how to answer your question.

LITERAL 97
LITERAL 98
MAX_REPEAT 0 MAXREPEAT
Expand All @@ -2350,7 +2350,7 @@ def test_repeat_index(self):
5: REPEAT 8 0 MAXREPEAT 0 (to 14)
10. LITERAL 0x61 ('a')
12. LITERAL 0x62 ('b')
14: MAX_UNTIL
14: MIN_UNTIL
15. REPEAT 8 0 MAXREPEAT 1 (to 24)
20. LITERAL 0x63 ('c')
22. LITERAL 0x64 ('d')
Expand Down
10 changes: 5 additions & 5 deletions Modules/_sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -1849,7 +1849,7 @@ _validate_inner(SRE_CODE *code, SRE_CODE *end, PatternObject *self)
case SRE_OP_REPEAT:
case SRE_OP_POSSESSIVE_REPEAT:
{
SRE_CODE op1 = op, min, max, repeat_index, _fields;
SRE_CODE op1 = op, min, max, repeat_index;
GET_SKIP;
GET_ARG; min = arg;
GET_ARG; max = arg;
Expand All @@ -1861,13 +1861,13 @@ _validate_inner(SRE_CODE *code, SRE_CODE *end, PatternObject *self)
GET_ARG; repeat_index = arg;
if (repeat_index >= (size_t)self->repeat_count)
FAIL;
_fields = 4;
skip -= 4;
} else {
_fields = 3;
skip -= 3;
}
if (!_validate_inner(code, code+skip-_fields, self))
if (!_validate_inner(code, code+skip, self))
FAIL;
code += skip-_fields;
code += skip;
GET_OP;
if (op1 == SRE_OP_POSSESSIVE_REPEAT) {
if (op != SRE_OP_SUCCESS)
Expand Down