ruby / strscan

Provides lexical scanning operations on a String.
BSD 2-Clause "Simplified" License
81 stars 32 forks source link

JRuby ext overflows slicing strings with non-zero begin #86

Closed headius closed 9 months ago

headius commented 9 months ago

When the string provided to StringScanner.new is represented by a ByteList with a non-zero begin offset, the length for new strings is calculated incorrectly (length and begin get added together). No tests exist for this case, which typically happens when the source string has itself been sliced off the end of a larger string and still shares the same buffer.

$ jruby -rstrscan -e 'str = "hellohello"[5..-1]; StringScanner.new(str).scan(/hello/)'
Unhandled Java exception: java.lang.ArrayIndexOutOfBoundsException: arraycopy: last source index 15 out of bounds for byte[10]
java.lang.ArrayIndexOutOfBoundsException: arraycopy: last source index 15 out of bounds for byte[10]
            arraycopy at java/lang/System.java:-2
               <init> at org/jruby/util/ByteList.java:227
            newString at org/jruby/ext/strscan/RubyStringScanner.java:877
        extractBegLen at org/jruby/ext/strscan/RubyStringScanner.java:260
                 scan at org/jruby/ext/strscan/RubyStringScanner.java:342
                 scan at org/jruby/ext/strscan/RubyStringScanner.java:391

This is the cause of jruby/jruby#8083. FIx coming.