Skip to content

[Bug #20228] Fix memory leak in Regexp timeout #9765

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

Merged
merged 3 commits into from
Feb 2, 2024

Conversation

peterzhu2118
Copy link
Member

If a Regexp::TimeoutError is raised, the stk_base and OnigRegion will leak.

For example:

Regexp.timeout = 0.001
regex = /^(a*)*$/
str = "a" * 1000000 + "x"

10.times do
  100.times do
    begin
      regex =~ str
    rescue
    end
  end

  puts `ps -o rss= -p #{$$}`
end

Before:

328800
632416
934368
1230448
1531088
1831248
2125072
2414384
2703440
2995664

After:

39280
47888
49024
56240
56496
56512
56592
56592
56720
56720

@peterzhu2118 peterzhu2118 force-pushed the regexp-timeout-mem-leak branch from 4f2a008 to 0f3dc8f Compare January 30, 2024 19:33
[Bug #20228]

If rb_reg_check_timeout raises a Regexp::TimeoutError, then the stk_base
will leak.
[Bug #20228]

rb_reg_onig_match can raise a Regexp::TimeoutError, which would cause
the OnigRegion to leak.
@peterzhu2118 peterzhu2118 force-pushed the regexp-timeout-mem-leak branch from 0f3dc8f to 1f435f9 Compare January 31, 2024 16:28
@peterzhu2118 peterzhu2118 merged commit 31378dc into ruby:master Feb 2, 2024
@peterzhu2118 peterzhu2118 deleted the regexp-timeout-mem-leak branch February 2, 2024 15:39
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.

1 participant