Commit d50c4e17

d50c4e177404Merged

Yasuhiro Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Guard against integer overflow in positional arg parsing

Details

Commit
d50c4e177404
Parent
b498c6baea03

lib/sp_runtime.hmodifiedView file

@@ -3571,9 +3571,13 @@
35713571 char spec[64]; size_t sl = 0; spec[sl++] = *p++;
35723572 /* positional argument reference: %N$conv selects the Nth (1-based) arg */
35733573 {
3574- const char *q = p; mrb_int argnum = 0;
3575- while (*q >= '0' && *q <= '9') { argnum = argnum * 10 + (*q - '0'); q++; }
3576- if (argnum > 0 && *q == '$') { idx = argnum - 1; p = q + 1; }
3574+ const char *q = p; mrb_int argnum = 0; mrb_bool overflow = FALSE;
3575+ while (*q >= '0' && *q <= '9') {
3576+ if (sp_int_mul_overflow_p(argnum, 10, &argnum) ||
3577+ sp_int_add_overflow_p(argnum, *q - '0', &argnum)) { overflow = TRUE; break; }
3578+ q++;
3579+ }
3580+ if (!overflow && argnum > 0 && *q == '$') { idx = argnum - 1; p = q + 1; }
35773581 }
35783582 while (*p && sl < sizeof(spec) - 4) {
35793583 char c = *p;