From 8685a81e6a0bdf7b766af930ee0e05a28e07d69b Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Fri, 2 May 2025 23:26:05 -0500 Subject: [PATCH] [ruby/strscan] jruby: Check if len++ walked off the end (https://github.com/ruby/strscan/pull/153) Fix https://github.com/ruby/strscan/pull/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 https://github.com/ruby/strscan/pull/127 but I missed adding this check due to a lack of tests. A test is included for both "-" and "+" parsing. https://github.com/ruby/strscan/commit/1abe4ca556 --- test/strscan/test_stringscanner.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/strscan/test_stringscanner.rb b/test/strscan/test_stringscanner.rb index 109f8e1f9a..085a911313 100644 --- a/test/strscan/test_stringscanner.rb +++ b/test/strscan/test_stringscanner.rb @@ -1001,6 +1001,16 @@ module StringScannerTests assert_equal(0, s.pos) refute_predicate(s, :matched?) + s = create_string_scanner('-') + assert_nil(s.scan_integer) + assert_equal(0, s.pos) + refute_predicate(s, :matched?) + + s = create_string_scanner('+') + assert_nil(s.scan_integer) + assert_equal(0, s.pos) + refute_predicate(s, :matched?) + huge_integer = '1' * 2_000 s = create_string_scanner(huge_integer) assert_equal(huge_integer.to_i, s.scan_integer)