Skip to content

Commit 53ab10a

Browse files
committed
Fix MatchData#[index, length] when index is larger than number of matched values
PullRequest: truffleruby/4290
2 parents 6192d04 + 8a46320 commit 53ab10a

File tree

4 files changed

+11
-1
lines changed

4 files changed

+11
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Bug fixes:
1515
* Fix `IO#{read_nonblock,readpartial,sysread}`, `BasicSocket#{recv,recv_nonblock}`, `{Socket,UDPSocket}#recvfrom_nonblock`, `UnixSocket#recvfrom` and preserve a provided buffer's encoding (#3506, @andrykonchyn).
1616
* Repair `IO#{wait_readable,wait_writable,wait}` to be interruptible (#3504, @andrykonchin).
1717
* Fix Hash value omission for constant names (@andrykonchin).
18+
* Fix `MatchData#[index, length]` when index is larger than number of matched values (@andrykonchin).
1819

1920
Compatibility:
2021

spec/ruby/core/matchdata/element_reference_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
# negative index is larger than the number of match values
2121
/(.)(.)(\d+)(\d)/.match("THX1138.")[-30, 2].should == nil
2222

23+
# positive index larger than number of match values
24+
/(.)(.)(\d+)(\d)/.match("THX1138.")[5, 2].should == []
25+
/(.)(.)(\d+)(\d)/.match("THX1138.")[6, 2].should == nil
26+
/(.)(.)(\d+)(\d)/.match("THX1138.")[30, 2].should == nil
27+
2328
# length argument larger than number of match values is capped to match value length
2429
/(.)(.)(\d+)(\d)/.match("THX1138.")[3, 10].should == %w|113 8|
2530

src/main/java/org/truffleruby/core/regexp/MatchDataNodes.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ Object getIndex(RubyMatchData matchData, int index, int length,
279279
@Cached @Exclusive InlinedConditionProfile negativeLengthProfile,
280280
@Cached @Shared InlinedConditionProfile normalizedIndexProfile,
281281
@Cached @Exclusive InlinedConditionProfile negativeIndexProfile,
282+
@Cached @Exclusive InlinedConditionProfile tooLargeIndexProfile,
282283
@Cached @Exclusive InlinedConditionProfile tooLargeTotalProfile) {
283284
final Object[] values = getValuesNode.execute(matchData);
284285

@@ -294,6 +295,10 @@ Object getIndex(RubyMatchData matchData, int index, int length,
294295
}
295296
}
296297

298+
if (tooLargeIndexProfile.profile(this, index > values.length)) {
299+
return nil;
300+
}
301+
297302
int endIndex = index + length;
298303
if (tooLargeTotalProfile.profile(this, endIndex > values.length)) {
299304
endIndex = values.length;

test/mri/excludes/TestRegexp.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
exclude :test_cache_optimization_exponential, "NoMethodError: undefined method `timeout=' for Regexp:Class"
2020
exclude :test_cache_optimization_square, "NoMethodError: undefined method `timeout=' for Regexp:Class"
2121
exclude :test_invalid_free_at_parse_depth_limit_over, "NameError: uninitialized constant Bug"
22-
exclude :test_match_aref, "throws internal exception"
2322
exclude :test_match_without_regexp, "Encoding::CompatibilityError: incompatible encoding regexp match (Shift_JIS regexp with UTF-8 string)"
2423
exclude :test_parse, "Polyglot::ForeignException: invalid group reference 80"
2524
exclude :test_parse_kg, "Polyglot::ForeignException: undefined name <-1> reference"

0 commit comments

Comments
 (0)