From f7777f64e6f8bbbe7048306e2834eb5726d2676b Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 11 Jun 2021 12:31:49 +0900 Subject: codegen.c: refactor `readint()` * renamed from redundant `readint_mrb_int()` * supports only base upto 16 * no base validation (already done in parser) * no negative read (negate after read) * overflow detection using `mrb_int_{mul,add}_overflow()` --- mrbgems/mruby-compiler/core/codegen.c | 45 +++++++++++++++-------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/mrbgems/mruby-compiler/core/codegen.c b/mrbgems/mruby-compiler/core/codegen.c index d271ca638..06696850f 100644 --- a/mrbgems/mruby-compiler/core/codegen.c +++ b/mrbgems/mruby-compiler/core/codegen.c @@ -1448,7 +1448,7 @@ raise_error(codegen_scope *s, const char *msg) } static mrb_int -readint_mrb_int(codegen_scope *s, const char *p, int base, mrb_bool neg, mrb_bool *overflow) +readint(codegen_scope *s, const char *p, int base, mrb_bool *overflow) { const char *e = p + strlen(p); mrb_int result = 0; @@ -1458,31 +1458,23 @@ readint_mrb_int(codegen_scope *s, const char *p, int base, mrb_bool neg, mrb_boo if (*p == '+') p++; while (p < e) { char c = *p; - c = tolower((unsigned char)c); - for (n=0; n result) { - *overflow = TRUE; - return 0; - } - result *= base; - result -= n; - } - else { - if ((MRB_INT_MAX - n)/base < result) { - *overflow = TRUE; - return 0; - } - result *= base; - result += n; + if (mrb_int_mul_overflow(result, base, &result) || + mrb_int_add_overflow(result, n, &result)) { + *overflow = TRUE; + return 0; } p++; } @@ -2536,7 +2528,7 @@ codegen(codegen_scope *s, node *tree, int val) mrb_int i; mrb_bool overflow; - i = readint_mrb_int(s, p, base, FALSE, &overflow); + i = readint(s, p, base, &overflow); if (overflow) { int off = new_litbn(s, p, base, FALSE); genop_bs(s, OP_LOADL, cursp(), off); @@ -2601,12 +2593,13 @@ codegen(codegen_scope *s, node *tree, int val) mrb_int i; mrb_bool overflow; - i = readint_mrb_int(s, p, base, TRUE, &overflow); + i = readint(s, p, base, &overflow); if (overflow) { int off = new_litbn(s, p, base, TRUE); genop_bs(s, OP_LOADL, cursp(), off); } else { + i = -i; if (i == -1) genop_1(s, OP_LOADI__1, cursp()); else if (i >= -0xff) { genop_2(s, OP_LOADINEG, cursp(), (uint16_t)-i); -- cgit v1.2.3