ruby-changes:71706
From: nagachika <ko1@a...>
Date: Tue, 12 Apr 2022 20:51:20 +0900 (JST)
Subject: [ruby-changes:71706] 052ec6d258 (ruby_3_0): Just free compiled pattern if no space is used
https://git.ruby-lang.org/ruby.git/commit/?id=052ec6d258 From 052ec6d2585c3ace95671013d336f5543624ef3d Mon Sep 17 00:00:00 2001 From: nagachika <nagachika@r...> Date: Tue, 12 Apr 2022 20:07:08 +0900 Subject: Just free compiled pattern if no space is used --- regcomp.c | 14 ++++++++------ test/ruby/test_regexp.rb | 9 +++++++++ version.h | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/regcomp.c b/regcomp.c index 3a438b94c4..bd38313875 100644 --- a/regcomp.c +++ b/regcomp.c @@ -142,8 +142,13 @@ bitset_on_num(BitSetRef bs) https://github.com/ruby/ruby/blob/trunk/regcomp.c#L142 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) https://github.com/ruby/ruby/blob/trunk/regcomp.c#L156 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 35d20eeda6..679a013cf0 100644 --- a/test/ruby/test_regexp.rb +++ b/test/ruby/test_regexp.rb @@ -1344,6 +1344,15 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L1344 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. def assert_match_at(re, str, positions, msg = nil) re = Regexp.new(re) unless re.is_a?(Regexp) diff --git a/version.h b/version.h index 0538f42411..5aee8a7013 100644 --- a/version.h +++ b/version.h @@ -12,7 +12,7 @@ https://github.com/ruby/ruby/blob/trunk/version.h#L12 # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR #define RUBY_VERSION_TEENY 4 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR -#define RUBY_PATCHLEVEL 206 +#define RUBY_PATCHLEVEL 207 #define RUBY_RELEASE_YEAR 2022 #define RUBY_RELEASE_MONTH 4 -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/