Skip to content

Commit

Permalink
invalid base shouldn't silently fail
Browse files Browse the repository at this point in the history
Revert 44a5809 and check range of
`base` with `mrb_assert`.
It actually can only be either 2, 8, 10 or 16 (and never 0), unless
mruby sources are modified. But I don't see a reason to impose further
limits.
  • Loading branch information
cremno committed Jul 9, 2014
1 parent 023908a commit ef59f55
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/codegen.c
Expand Up @@ -1095,6 +1095,7 @@ readint_mrb_int(codegen_scope *s, const char *p, int base, mrb_bool neg, mrb_boo
mrb_int result = 0;
int n;

mrb_assert(base >= 2 && base <= 36);
if (*p == '+') p++;
while (p < e) {
char c = *p;
Expand All @@ -1108,23 +1109,21 @@ readint_mrb_int(codegen_scope *s, const char *p, int base, mrb_bool neg, mrb_boo
codegen_error(s, "malformed readint input");
}

if(base > 0) {
if (neg) {
if ((MRB_INT_MIN + n)/base > result) {
*overflow = TRUE;
return 0;
}
result *= base;
result -= n;
if (neg) {
if ((MRB_INT_MIN + n)/base > result) {
*overflow = TRUE;
return 0;
}
else {
if ((MRB_INT_MAX - n)/base < result) {
*overflow = TRUE;
return 0;
}
result *= base;
result += n;
result *= base;
result -= n;
}
else {
if ((MRB_INT_MAX - n)/base < result) {
*overflow = TRUE;
return 0;
}
result *= base;
result += n;
}
p++;
}
Expand Down

0 comments on commit ef59f55

Please sign in to comment.