Project

General

Profile

Actions

Bug #20228

closed

Memory leak in Regexp timeout

Added by peterzhu2118 (Peter Zhu) 6 months ago. Updated 14 days ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:116514]

Description

GitHub PR: https://github.com/ruby/ruby/pull/9765

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

Updated by nobu (Nobuyoshi Nakada) 6 months ago

Using ruby APIs in onigmo doesn’t feel nice.

Updated by mame (Yusuke Endoh) 6 months ago

Good find, thanks!

I'm a little concerned about the overhead of rb_protect for a typical simple match, but it's neglectable?

Updated by peterzhu2118 (Peter Zhu) 6 months ago

Using ruby APIs in onigmo doesn’t feel nice.

I changed it to call HANDLE_REG_TIMEOUT_IN_MATCH_AT in onigmo, which calls rb_reg_raise_timeout so then there is no Ruby code in onigmo.

I'm a little concerned about the overhead of rb_protect for a typical simple match, but it's neglectable?

I think it can only raise when there is a timeout set, so I changed the implementation to only use rb_protect when there is a timeout.

Actions #4

Updated by peterzhu2118 (Peter Zhu) 6 months ago

  • Status changed from Open to Closed

Applied in changeset git|1c120efe02d079b0a1dea573cf0fd7978d9cc857.


Fix memory leak in stk_base when Regexp timeout

[Bug #20228]

If rb_reg_check_timeout raises a Regexp::TimeoutError, then the stk_base
will leak.

Updated by naruse (Yui NARUSE) 4 months ago

  • Backport changed from 3.0: UNKNOWN, 3.1: DONTNEED, 3.2: REQUIRED, 3.3: REQUIRED to 3.0: UNKNOWN, 3.1: DONTNEED, 3.2: REQUIRED, 3.3: DONE

ruby_3_3 c626c201e4129bbea17583ecef73472c6f668c81 merged revision(s) 01bfd1a2bf013a9ed92a9722ac5228187e05e6a8,1c120efe02d079b0a1dea573cf0fd7978d9cc857,31378dc0969f4466b2122d730b7298dd7004acdf.

Updated by nagachika (Tomoyuki Chikanaga) 14 days ago

  • Backport changed from 3.0: UNKNOWN, 3.1: DONTNEED, 3.2: REQUIRED, 3.3: DONE to 3.0: UNKNOWN, 3.1: DONTNEED, 3.2: WONTFIX, 3.3: DONE

I gave up to make a clean patch for ruby_3_2 branch. Please make PR if you want to backport.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like1Like0Like0Like0