summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2020-11-04 14:27:34 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2020-11-04 14:27:34 +0900
commitaa4d8e9a2b41e578659247e4da3a881a12853999 (patch)
treee9b139b928916337450358499e5e55c8988f82d6
parent0113db6716f5ede302e2fca2e0888107ce15eb07 (diff)
downloadmruby-aa4d8e9a2b41e578659247e4da3a881a12853999.tar.gz
mruby-aa4d8e9a2b41e578659247e4da3a881a12853999.zip
Avoid integer overflow in comparison.
-rw-r--r--mrbgems/mruby-compiler/core/codegen.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/mrbgems/mruby-compiler/core/codegen.c b/mrbgems/mruby-compiler/core/codegen.c
index 1af071fdb..d5f8f8bb0 100644
--- a/mrbgems/mruby-compiler/core/codegen.c
+++ b/mrbgems/mruby-compiler/core/codegen.c
@@ -2487,8 +2487,12 @@ codegen(codegen_scope *s, node *tree, int val)
if (i == -1) genop_1(s, OP_LOADI__1, cursp());
else if (i >= -0xff) genop_2(s, OP_LOADINEG, cursp(), (uint16_t)-i);
else if (i >= -0x8000) genop_2S(s, OP_LOADI16, cursp(), (uint16_t)i);
- else if (i >= -0x80000000) genop_2SS(s, OP_LOADI32, cursp(), (uint32_t)i);
+#ifdef MRB_INT64
+ else if (i >= -(int32_t)0x80000000) genop_2SS(s, OP_LOADI32, cursp(), (uint32_t)i);
else goto lit_int;
+#else
+ else genop_2SS(s, OP_LOADI32, cursp(), (uint32_t)i);
+#endif
}
else if (i < 8) genop_1(s, OP_LOADI_0 + (uint8_t)i, cursp());
else if (i <= 0xff) genop_2(s, OP_LOADI, cursp(), (uint16_t)i);
@@ -2497,7 +2501,9 @@ codegen(codegen_scope *s, node *tree, int val)
else {
int off;
+#ifdef MRB_INT64
lit_int:
+#endif
off = new_lit(s, mrb_int_value(s->mrb, i));
genop_bs(s, OP_LOADL, cursp(), off);
}
@@ -2560,6 +2566,9 @@ codegen(codegen_scope *s, node *tree, int val)
else if (i >= -0x8000) {
genop_2S(s, OP_LOADI16, cursp(), (uint16_t)i);
}
+#ifdef MRB_INT32
+ else genop_2SS(s, OP_LOADI32, cursp(), (uint32_t)i);
+#else
else if (i >= -0x80000000) {
genop_2SS(s, OP_LOADI32, cursp(), (uint32_t)i);
}
@@ -2567,6 +2576,7 @@ codegen(codegen_scope *s, node *tree, int val)
int off = new_lit(s, mrb_int_value(s->mrb, i));
genop_bs(s, OP_LOADL, cursp(), off);
}
+#endif
#ifndef MRB_NO_FLOAT
}
#endif