diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2021-06-11 00:06:43 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2022-04-12 20:24:14 +0900 |
commit | cf2bbcfff2985c116552967c7c4522f4630f2d18 (patch) | |
tree | 5c1d9427852cdb2c6eff34917b7e4ca277c26790 | |
parent | 48ffa2804431ea24fa7a3b6f5470122b0128ba22 (diff) |
Just free compiled pattern if no space is used
https://hackerone.com/reports/1220911
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/5793
-rw-r--r-- | regcomp.c | 14 | ||||
-rw-r--r-- | test/ruby/test_regexp.rb | 9 |
2 files changed, 17 insertions, 6 deletions
@@ -142,8 +142,13 @@ bitset_on_num(BitSetRef bs) static void onig_reg_resize(regex_t *reg) { - resize: - if (reg->alloc > reg->used) { + do { + if (!reg->used) { + xfree(reg->p); + reg->alloc = 0; + reg->p = 0; + } + else if (reg->alloc > reg->used) { unsigned char *new_ptr = xrealloc(reg->p, reg->used); // Skip the right size optimization if memory allocation fails if (new_ptr) { @@ -151,10 +156,7 @@ onig_reg_resize(regex_t *reg) reg->p = new_ptr; } } - if (reg->chain) { - reg = reg->chain; - goto resize; - } + } while ((reg = reg->chain) != 0); } extern int diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb index 4be6d7bec7..84687c5380 100644 --- a/test/ruby/test_regexp.rb +++ b/test/ruby/test_regexp.rb @@ -1431,6 +1431,15 @@ class TestRegexp < Test::Unit::TestCase assert_kind_of MatchData, /(?<x>a)(?<x>aa)\k<x>/.match("aaaab") end + def test_invalid_group + assert_separately([], "#{<<-"begin;"}\n#{<<-'end;'}") + begin; + assert_raise_with_message(RegexpError, /invalid conditional pattern/) do + Regexp.new("((?(1)x|x|)x)+") + end + end; + end + # This assertion is for porting x2() tests in testpy.py of Onigmo. def assert_match_at(re, str, positions, msg = nil) re = Regexp.new(re) unless re.is_a?(Regexp) |