mrb_str_len_to_inum(): string may not be NUL terminated; ref #3043

parent 19c744e1
...@@ -2124,6 +2124,10 @@ mrb_str_len_to_inum(mrb_state *mrb, const char *str, size_t len, int base, int b ...@@ -2124,6 +2124,10 @@ mrb_str_len_to_inum(mrb_state *mrb, const char *str, size_t len, int base, int b
} }
break; break;
} /* end of switch (base) { */ } /* end of switch (base) { */
if (p>=pend) {
if (badcheck) goto bad;
return mrb_fixnum_value(0);
}
if (*p == '0') { /* squeeze preceding 0s */ if (*p == '0') { /* squeeze preceding 0s */
p++; p++;
while (p<pend) { while (p<pend) {
...@@ -2153,14 +2157,17 @@ mrb_str_len_to_inum(mrb_state *mrb, const char *str, size_t len, int base, int b ...@@ -2153,14 +2157,17 @@ mrb_str_len_to_inum(mrb_state *mrb, const char *str, size_t len, int base, int b
for ( ;p<pend;p++) { for ( ;p<pend;p++) {
if (*p == '_') { if (*p == '_') {
if (p[1] == '_') { if (p+1<pend && p[1] == '_') {
if (badcheck) goto bad; if (badcheck) goto bad;
continue; continue;
} }
p++; p++;
if (badcheck && p<pend)
goto bad;
} }
if (badcheck && *p == '\0') { if (badcheck && *p == '\0') {
goto nullbyte; goto nullbyte;
break;
} }
c = conv_digit(*p); c = conv_digit(*p);
if (c < 0 || c >= base) { if (c < 0 || c >= base) {
...@@ -2186,7 +2193,7 @@ mrb_str_len_to_inum(mrb_state *mrb, const char *str, size_t len, int base, int b ...@@ -2186,7 +2193,7 @@ mrb_str_len_to_inum(mrb_state *mrb, const char *str, size_t len, int base, int b
/* not reached */ /* not reached */
bad: bad:
mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid string for number(%S)", mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid string for number(%S)",
mrb_inspect(mrb, mrb_str_new_cstr(mrb, str))); mrb_inspect(mrb, mrb_str_new(mrb, str, pend-str)));
/* not reached */ /* not reached */
return mrb_fixnum_value(0); return mrb_fixnum_value(0);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment