Ensure the byte position is a valid boundary

This commit is contained in:
Nobuyoshi Nakada 2023-06-28 09:28:15 +09:00
parent 6528cf9fcf
commit 3d7a6bbc12
Notes: git 2023-06-28 13:42:23 +00:00

View File

@ -3907,18 +3907,21 @@ rb_str_index_m(int argc, VALUE *argv, VALUE str)
return LONG2NUM(pos); return LONG2NUM(pos);
} }
/* whether given pos is valid character boundary or not /* Ensure that the given pos is a valid character boundary.
* Note that in this function, "character" means a code point * Note that in this function, "character" means a code point
* (Unicode scalar value), not a grapheme cluster. * (Unicode scalar value), not a grapheme cluster.
*/ */
static bool static void
str_check_byte_pos(VALUE str, long pos) str_ensure_byte_pos(VALUE str, long pos)
{ {
const char *s = RSTRING_PTR(str); const char *s = RSTRING_PTR(str);
const char *e = RSTRING_END(str); const char *e = RSTRING_END(str);
const char *p = s + pos; const char *p = s + pos;
const char *pp = rb_enc_left_char_head(s, p, e, rb_enc_get(str)); const char *pp = rb_enc_left_char_head(s, p, e, rb_enc_get(str));
return p == pp; if (p != pp) {
rb_raise(rb_eIndexError,
"offset %ld does not land on character boundary", pos);
}
} }
/* /*
@ -3986,10 +3989,7 @@ rb_str_byteindex_m(int argc, VALUE *argv, VALUE str)
pos = 0; pos = 0;
} }
if (!str_check_byte_pos(str, pos)) { str_ensure_byte_pos(str, pos);
rb_raise(rb_eIndexError,
"offset %ld does not land on character boundary", pos);
}
if (RB_TYPE_P(sub, T_REGEXP)) { if (RB_TYPE_P(sub, T_REGEXP)) {
if (rb_reg_search(sub, str, pos, 0) < 0) { if (rb_reg_search(sub, str, pos, 0) < 0) {
@ -4316,10 +4316,7 @@ rb_str_byterindex_m(int argc, VALUE *argv, VALUE str)
pos = len; pos = len;
} }
if (!str_check_byte_pos(str, pos)) { str_ensure_byte_pos(str, pos);
rb_raise(rb_eIndexError,
"offset %ld does not land on character boundary", pos);
}
if (RB_TYPE_P(sub, T_REGEXP)) { if (RB_TYPE_P(sub, T_REGEXP)) {
if (rb_reg_search(sub, str, pos, 1) >= 0) { if (rb_reg_search(sub, str, pos, 1) >= 0) {
@ -6197,14 +6194,8 @@ str_check_beg_len(VALUE str, long *beg, long *len)
*len = slen - *beg; *len = slen - *beg;
} }
end = *beg + *len; end = *beg + *len;
if (!str_check_byte_pos(str, *beg)) { str_ensure_byte_pos(str, *beg);
rb_raise(rb_eIndexError, str_ensure_byte_pos(str, end);
"offset %ld does not land on character boundary", *beg);
}
if (!str_check_byte_pos(str, end)) {
rb_raise(rb_eIndexError,
"offset %ld does not land on character boundary", end);
}
} }
/* /*