1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Just free compiled pattern if no space is used

https://hackerone.com/reports/1220911
This commit is contained in:
Nobuyoshi Nakada 2021-06-11 00:06:43 +09:00
parent 48ffa28044
commit cf2bbcfff2
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6
Notes: git 2022-04-12 21:25:49 +09:00
2 changed files with 17 additions and 6 deletions

View file

@ -142,8 +142,13 @@ bitset_on_num(BitSetRef bs)
static void static void
onig_reg_resize(regex_t *reg) onig_reg_resize(regex_t *reg)
{ {
resize: do {
if (reg->alloc > reg->used) { 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); unsigned char *new_ptr = xrealloc(reg->p, reg->used);
// Skip the right size optimization if memory allocation fails // Skip the right size optimization if memory allocation fails
if (new_ptr) { if (new_ptr) {
@ -151,10 +156,7 @@ onig_reg_resize(regex_t *reg)
reg->p = new_ptr; reg->p = new_ptr;
} }
} }
if (reg->chain) { } while ((reg = reg->chain) != 0);
reg = reg->chain;
goto resize;
}
} }
extern int extern int

View file

@ -1431,6 +1431,15 @@ class TestRegexp < Test::Unit::TestCase
assert_kind_of MatchData, /(?<x>a)(?<x>aa)\k<x>/.match("aaaab") assert_kind_of MatchData, /(?<x>a)(?<x>aa)\k<x>/.match("aaaab")
end 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. # This assertion is for porting x2() tests in testpy.py of Onigmo.
def assert_match_at(re, str, positions, msg = nil) def assert_match_at(re, str, positions, msg = nil)
re = Regexp.new(re) unless re.is_a?(Regexp) re = Regexp.new(re) unless re.is_a?(Regexp)