Skip to content

Commit 1abe4ca

Browse files
authored
jruby: Check if len++ walked off the end (#153)
Fix GH-152 CRuby can walk off the end because there's always a null byte. In JRuby, the byte array is often (usually?) the exact size of the string. So we need to check if len++ walked off the end. This code was ported from a version by @byroot in #127 but I missed adding this check due to a lack of tests. A test is included for both "-" and "+" parsing.
1 parent a4afd32 commit 1abe4ca

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ public IRubyObject scan_base10_integer(ThreadContext context) {
589589
len++;
590590
}
591591

592-
if (!Character.isDigit(bytes.get(ptr + len))) {
592+
if (len >= remaining_len || !Character.isDigit(bytes.get(ptr + len))) {
593593
return context.nil;
594594
}
595595

test/strscan/test_stringscanner.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,16 @@ def test_scan_integer
10011001
assert_equal(0, s.pos)
10021002
refute_predicate(s, :matched?)
10031003

1004+
s = create_string_scanner('-')
1005+
assert_nil(s.scan_integer)
1006+
assert_equal(0, s.pos)
1007+
refute_predicate(s, :matched?)
1008+
1009+
s = create_string_scanner('+')
1010+
assert_nil(s.scan_integer)
1011+
assert_equal(0, s.pos)
1012+
refute_predicate(s, :matched?)
1013+
10041014
huge_integer = '1' * 2_000
10051015
s = create_string_scanner(huge_integer)
10061016
assert_equal(huge_integer.to_i, s.scan_integer)

0 commit comments

Comments
 (0)