diff options
author | Nobuyoshi Nakada <[email protected]> | 2023-06-28 14:44:01 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <[email protected]> | 2023-06-28 17:23:32 +0900 |
commit | bc3ac1872e4523334e3ed04c2bb70a55c4c43f98 (patch) | |
tree | b2b1359942b304f7cd763eba3068a05730179b54 /string.c | |
parent | 715c5ca4a41cc4c5e74729b4eed21b9465447d52 (diff) |
[Bug #19748] Fix out-of-bound access in `String#byteindex`
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 17 |
1 files changed, 7 insertions, 10 deletions
@@ -3970,20 +3970,21 @@ rb_str_byteindex_m(int argc, VALUE *argv, VALUE str) long pos; if (rb_scan_args(argc, argv, "11", &sub, &initpos) == 2) { + long slen = RSTRING_LEN(str); pos = NUM2LONG(initpos); - } - else { - pos = 0; - } - if (pos < 0) { - pos += RSTRING_LEN(str); if (pos < 0) { + pos += slen; + } + if (pos < 0 || pos > slen) { if (RB_TYPE_P(sub, T_REGEXP)) { rb_backref_set(Qnil); } return Qnil; } } + else { + pos = 0; + } if (!str_check_byte_pos(str, pos)) { rb_raise(rb_eIndexError, @@ -3991,10 +3992,6 @@ rb_str_byteindex_m(int argc, VALUE *argv, VALUE str) } if (RB_TYPE_P(sub, T_REGEXP)) { - if (pos > RSTRING_LEN(str)) { - rb_backref_set(Qnil); - return Qnil; - } if (rb_reg_search(sub, str, pos, 0) < 0) { return Qnil; } |