diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-08-02 14:10:38 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-08-02 14:10:38 +0900 |
| commit | e93642d53fe15c86a9ff7c62e8712ad9c86bef34 (patch) | |
| tree | be4483609b35c67b8c638f1bd2ccf8534d7a654a /mrbgems/mruby-compiler/core/codegen.c | |
| parent | 6ea0c04c0616e55de93e77ba45b9d9933b9460e8 (diff) | |
| download | mruby-e93642d53fe15c86a9ff7c62e8712ad9c86bef34.tar.gz mruby-e93642d53fe15c86a9ff7c62e8712ad9c86bef34.zip | |
codegen.c: refactor `readint()` to read `MRB_INT_MIN`.
Diffstat (limited to 'mrbgems/mruby-compiler/core/codegen.c')
| -rw-r--r-- | mrbgems/mruby-compiler/core/codegen.c | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/mrbgems/mruby-compiler/core/codegen.c b/mrbgems/mruby-compiler/core/codegen.c index 00cdc0059..65d14bcf6 100644 --- a/mrbgems/mruby-compiler/core/codegen.c +++ b/mrbgems/mruby-compiler/core/codegen.c @@ -1812,12 +1812,12 @@ raise_error(codegen_scope *s, const char *msg) } static mrb_int -readint(codegen_scope *s, const char *p, int base, mrb_bool *overflow) +readint(codegen_scope *s, const char *p, int base, mrb_bool neg, mrb_bool *overflow) { const char *e = p + strlen(p); mrb_int result = 0; - mrb_assert(base >= 2 && base <= 36); + mrb_assert(base >= 2 && base <= 16); if (*p == '+') p++; while (p < e) { int n; @@ -1838,14 +1838,22 @@ readint(codegen_scope *s, const char *p, int base, mrb_bool *overflow) /* not reached */ return result; } - if (mrb_int_mul_overflow(result, base, &result) || - mrb_int_add_overflow(result, n, &result)) { + if (mrb_int_mul_overflow(result, base, &result)) { + overflow: *overflow = TRUE; return 0; } + mrb_uint tmp = ((mrb_uint)result)+n; + if (neg && tmp == (mrb_uint)MRB_INT_MAX+1) { + *overflow = FALSE; + return MRB_INT_MIN; + } + if (tmp > MRB_INT_MAX) goto overflow; + result = (mrb_int)tmp; p++; } *overflow = FALSE; + if (neg) return -result; return result; } @@ -2904,7 +2912,7 @@ codegen(codegen_scope *s, node *tree, int val) mrb_int i; mrb_bool overflow; - i = readint(s, p, base, &overflow); + i = readint(s, p, base, FALSE, &overflow); if (overflow) { int off = new_litbn(s, p, base, FALSE); genop_2(s, OP_LOADL, cursp(), off); @@ -2953,13 +2961,13 @@ codegen(codegen_scope *s, node *tree, int val) mrb_int i; mrb_bool overflow; - i = readint(s, p, base, &overflow); + i = readint(s, p, base, TRUE, &overflow); if (overflow) { int off = new_litbn(s, p, base, TRUE); genop_2(s, OP_LOADL, cursp(), off); } else { - gen_int(s, cursp(), -i); + gen_int(s, cursp(), i); } push(); } |
