Check negative integer underflow

This commit is contained in:
Nobuyoshi Nakada 2024-11-26 10:55:49 +09:00 committed by Nobuyoshi Nakada
parent c08e1f336c
commit 02b70256b5
Notes: git 2024-11-26 02:46:55 +00:00
2 changed files with 9 additions and 3 deletions

View File

@ -3053,11 +3053,12 @@ rb_str_subpos(VALUE str, long beg, long *lenp)
{
long len = *lenp;
long slen = -1L;
long blen = RSTRING_LEN(str);
const long blen = RSTRING_LEN(str);
rb_encoding *enc = STR_ENC_GET(str);
char *p, *s = RSTRING_PTR(str), *e = s + blen;
if (len < 0) return 0;
if (beg < 0 && -beg < 0) return 0;
if (!blen) {
len = 0;
}
@ -3075,7 +3076,7 @@ rb_str_subpos(VALUE str, long beg, long *lenp)
}
if (beg < 0) {
if (len > -beg) len = -beg;
if (-beg * rb_enc_mbmaxlen(enc) < RSTRING_LEN(str) / 8) {
if (-beg * rb_enc_mbmaxlen(enc) < blen / 8) {
beg = -beg;
while (beg-- > len && (e = rb_enc_prev_char(s, e, e, enc)) != 0);
p = e;
@ -3093,7 +3094,7 @@ rb_str_subpos(VALUE str, long beg, long *lenp)
if (len == 0) goto end;
}
}
else if (beg > 0 && beg > RSTRING_LEN(str)) {
else if (beg > 0 && beg > blen) {
return 0;
}
if (len == 0) {

View File

@ -164,6 +164,11 @@ CODE
assert_raise(ArgumentError) { "foo"[] }
end
def test_AREF_underflow
require "rbconfig/sizeof"
assert_equal(nil, S("\u{3042 3044 3046}")[RbConfig::LIMITS["LONG_MIN"], 1])
end
def test_ASET # '[]='
s = S("FooBar")
s[0] = S('A')