oracle / truffleruby

A high performance implementation of the Ruby programming language, built on GraalVM.
https://www.graalvm.org/ruby/
Other
3.01k stars 184 forks source link

MatchData#[] with an unbounded Range not supported #2755

Closed postmodern closed 1 year ago

postmodern commented 1 year ago

It does not appear that MatchData#[] supports accepting an unbounded range of indices.

Steps To Reproduce

string = "aaaabababab"
regexp = /(ab)(ab)(ab)/
match = string.match(regexp)
p match[1..]

Expected Result

["ab", "ab", "ab"]

Actual Result

<internal:core> core/type.rb:276:in `convert_type': no implicit conversion of Range into Integer (TypeError)
    from <internal:core> core/type.rb:178:in `rb_to_int_fallback'
    from (irb):4:in `[]'
    from (irb):4:in `<top (required)>'
    from <internal:core> core/kernel.rb:407:in `loop'
    from <internal:core> core/throw_catch.rb:36:in `catch'
    from <internal:core> core/throw_catch.rb:36:in `catch'
    from /opt/rubies/truffleruby-22.2.0/lib/gems/gems/irb-1.3.5/exe/irb:11:in `<top (required)>'
    from <internal:core> core/kernel.rb:376:in `load'
    from <internal:core> core/kernel.rb:376:in `load'
    from /opt/rubies/truffleruby-22.2.0/bin/irb:42:in `<main>'

Version Information

nirvdrum commented 1 year ago

Unfortunately, that error message isn't entirely helpful. We do support ranges, but only when both the start and end values are specified.

string = "aaaabababab"
regexp = /(ab)(ab)(ab)/
match = string.match(regexp)
p match[1..3] # => ["ab", "ab", "ab"]

There's nothing in the Ruby Spec Suite for this, so it's likely an oversight. We'll fix the issue, but if that's a blocker, you can avoid it by specifying the end value in the range, providing you have it.

eregon commented 1 year ago

As a note, there is MatchData#captures for this exact pattern (from https://github.com/ronin-rb/ronin-vulns/commit/b7f472e527619f6fab70e8a44d0895f8904214a9#r86454351)

andrykonchin commented 1 year ago

Thank you for report. Fixed in b7d7cd0890896672c587bbb9277b00f7bb468c70.