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

gh-67877: Fix memory leaks in terminated RE matching #126840

Merged
merged 5 commits into from
Nov 18, 2024

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Nov 14, 2024

If SRE(match) function terminates abruptly, either because of a signal or because memory allocation fails, allocated SRE_REPEAT blocks might be never released.

If SRE(match) function terminates abruptly, either because of a signal
or because memory allocation fails, allocated SRE_REPEAT blocks might
be never released.
@serhiy-storchaka
Copy link
Member Author

See also #126843.

Modules/_sre/sre.c Outdated Show resolved Hide resolved
Modules/_sre/sre.c Outdated Show resolved Hide resolved
Modules/_sre/sre.c Show resolved Hide resolved
Modules/_sre/sre.c Outdated Show resolved Hide resolved
Modules/_sre/sre.c Show resolved Hide resolved
@wjssz
Copy link

wjssz commented Nov 16, 2024

Looks good to me.
You may also remove the outdated SRE_ERROR_RECURSION_LIMIT at the same time, or remove it later.

do { \
_MAYBE_CHECK_SIGNALS; \
if (state->fail_after_count >= 0) { \
if (state->fail_after_count-- == 0) { \
Copy link
Member Author

Choose a reason for hiding this comment

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

Setting state->fail_after_count does not affect subsequent calls of the Pattern methods, but it still affects the finditer() iterator which reuses state between iterations.

Copy link

@wjssz wjssz Nov 16, 2024

Choose a reason for hiding this comment

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

I deleted my previous reply.
Maybe setting state->fail_after_count = -1 doesn't make sense, it's better to make the finditer() iterator stop the subsequent matchings.

@wjssz
Copy link

wjssz commented Nov 16, 2024

I remember another thing, if this patch is going to be backported, you may remove the changes related to this:

-    PyObject* pattern;	
+    PatternObject* pattern;

The patch will be smaller and clearer.
Polishing work can be done only on the main branch, such as change the type, remove the outdated SRE_ERROR_RECURSION_LIMIT.

@serhiy-storchaka serhiy-storchaka merged commit 7538e7f into python:main Nov 18, 2024
39 checks passed
@miss-islington-app
Copy link

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

@serhiy-storchaka serhiy-storchaka deleted the re-memleaks branch November 18, 2024 09:53
@miss-islington-app
Copy link

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 7538e7f5696408fa0aa02fce8a413a7dfac76a04 3.13

@miss-islington-app
Copy link

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 7538e7f5696408fa0aa02fce8a413a7dfac76a04 3.12

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Nov 18, 2024
…126840)

If SRE(match) function terminates abruptly, either because of a signal
or because memory allocation fails, allocated SRE_REPEAT blocks might
be never released.

Co-authored-by: <wjssz@users.noreply.github.com>
(cherry picked from commit 7538e7f)
@bedevere-app
Copy link

bedevere-app bot commented Nov 18, 2024

GH-126960 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Nov 18, 2024
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Nov 18, 2024
…thonGH-126840)

If SRE(match) function terminates abruptly, either because of a signal
or because memory allocation fails, allocated SRE_REPEAT blocks might
be never released.

(cherry picked from commit 7538e7f)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: <wjssz@users.noreply.github.com>
@bedevere-app
Copy link

bedevere-app bot commented Nov 18, 2024

GH-126961 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Nov 18, 2024
@serhiy-storchaka serhiy-storchaka removed their assignment Nov 18, 2024
@serhiy-storchaka
Copy link
Member Author

I remember another thing, if this patch is going to be backported, you may remove the changes related to this:

It is easier to not do. I can introduce new bugs when trying to remove these changes in backports.

Initially it was needed to pass the second argument to state_fini, after reverting that change it is just a clean up, but it can still be helpful if in future we will need to pass the second argument to state_fini.

serhiy-storchaka added a commit that referenced this pull request Nov 18, 2024
… (GH-126961)

If SRE(match) function terminates abruptly, either because of a signal
or because memory allocation fails, allocated SRE_REPEAT blocks might
be never released.

(cherry picked from commit 7538e7f)
serhiy-storchaka added a commit that referenced this pull request Nov 18, 2024
… (GH-126960)

If SRE(match) function terminates abruptly, either because of a signal
or because memory allocation fails, allocated SRE_REPEAT blocks might
be never released.

Co-authored-by: <wjssz@users.noreply.github.com>
(cherry picked from commit 7538e7f)
@wjssz
Copy link

wjssz commented Nov 19, 2024

sre only has the “\B can't match empty string” bug as far as I know. It's easy to fix, if decide to fix it.
Maybe people won't touch sre's code for many years.

ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
…126840)

If SRE(match) function terminates abruptly, either because of a signal
or because memory allocation fails, allocated SRE_REPEAT blocks might
be never released.

Co-authored-by: <wjssz@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants