string.c (sub_replace): check if found argument is within proper range

Since it is an internal method, we didn't check the argument range, but
if it's called directly, out-of-range argument could cause SEGV.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-04-15 07:11:09 +09:00
parent 9cc6d39956
commit 8b213c4fbf
+3
View File
@@ -3046,6 +3046,9 @@ sub_replace(mrb_state *mrb, mrb_value self)
mrb_value result;
mrb_get_args(mrb, "ssi", &p, &plen, &match, &mlen, &found);
if (found < 0 || RSTRING_LEN(self) < found) {
mrb_raise(mrb, E_RUNTIME_ERROR, "argument out of range");
}
result = mrb_str_new(mrb, 0, 0);
for (mrb_int i=0; i<plen; i++) {
if (p[i] != '\\' || i+1==plen) {