numeric.c: fix a bug in left shift of negative integer.

`-1 * (1<<63)` causes overflow, but `-1<<63` is a valid value.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2021-08-04 12:05:09 +09:00
parent 9b3b2a4c95
commit 9f37733382
+5 -2
View File
@@ -1332,10 +1332,13 @@ mrb_num_shift(mrb_state *mrb, mrb_int val, mrb_int width, mrb_int *num)
}
else {
if ((width > NUMERIC_SHIFT_WIDTH_MAX) ||
(val <= (MRB_INT_MIN >> width))) {
(val < (MRB_INT_MIN >> width))) {
return FALSE;
}
*num = val * ((mrb_int)1 << width);
if (width == NUMERIC_SHIFT_WIDTH_MAX)
*num = MRB_INT_MIN;
else
*num = val * ((mrb_int)1 << width);
}
return TRUE;
}