Avoid undefined behavior of left shifting negative integer; #3728

parent c377d504
...@@ -962,16 +962,16 @@ lshift(mrb_state *mrb, mrb_int val, mrb_int width) ...@@ -962,16 +962,16 @@ lshift(mrb_state *mrb, mrb_int val, mrb_int width)
(val > (MRB_INT_MAX >> width))) { (val > (MRB_INT_MAX >> width))) {
goto bit_overflow; goto bit_overflow;
} }
return mrb_fixnum_value(val << width);
} }
else { else {
if ((width > NUMERIC_SHIFT_WIDTH_MAX) || if ((width > NUMERIC_SHIFT_WIDTH_MAX) ||
(val < (MRB_INT_MIN >> width))) { (val < (MRB_INT_MIN >> width))) {
goto bit_overflow; goto bit_overflow;
} }
return mrb_fixnum_value(val * (1u << width));
} }
return mrb_fixnum_value(val << width);
bit_overflow: bit_overflow:
{ {
mrb_float f = (mrb_float)val; mrb_float f = (mrb_float)val;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment