Skip to content

Commit 3032e38

Browse files
authored
Add missing shrunk check in integer_at (#212)
1 parent 9a94ebe commit 3032e38

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

ext/strscan/strscan.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,8 +1862,13 @@ strscan_integer_at(int argc, VALUE *argv, VALUE self)
18621862
return Qnil;
18631863

18641864
beg = adjust_register_position(p, p->regs.beg[i]);
1865+
if (beg > S_LEN(p))
1866+
return Qnil;
18651867
end = adjust_register_position(p, p->regs.end[i]);
1868+
end = minl(end, S_LEN(p));
18661869
len = end - beg;
1870+
if (len == 0)
1871+
return Qnil;
18671872
ptr = S_PBEG(p) + beg;
18681873
#ifdef HAVE_RB_INT_PARSE_CSTR
18691874
{

test/strscan/test_stringscanner.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,26 @@ def test_integer_at_base_auto
578578
assert_integer_at(s, 0, 0) # 0xaf
579579
end
580580

581+
def test_integer_at_shrunk
582+
omit("not supported on TruffleRuby") if RUBY_ENGINE == "truffleruby"
583+
584+
s = create_string_scanner(+"before 29 after")
585+
s.skip_until(" ")
586+
assert_equal("29", s.scan(/\d+/))
587+
s.string.replace("before ")
588+
assert_nil(s.integer_at(0))
589+
end
590+
591+
def test_integer_at_shrunk_partial
592+
omit("not supported on TruffleRuby") if RUBY_ENGINE == "truffleruby"
593+
594+
s = create_string_scanner(+"before 29 after")
595+
s.skip_until(" ")
596+
assert_equal("29", s.scan(/\d+/))
597+
s.string.replace("before 2")
598+
assert_integer_at(s, 2)
599+
end
600+
581601
def test_pre_match
582602
s = create_string_scanner('a b c d e')
583603
s.scan(/\w/)

0 commit comments

Comments
 (0)