diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-08-04 12:05:09 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-08-04 12:05:09 +0900 |
| commit | 9f377333825fe5ae00a6fa757da150bee5897b49 (patch) | |
| tree | c404ff929c921887e201764a109ab6dc639a49ba /src/numeric.c | |
| parent | 9b3b2a4c957bbeb0b65978e8075c348cdecda44f (diff) | |
| download | mruby-9f377333825fe5ae00a6fa757da150bee5897b49.tar.gz mruby-9f377333825fe5ae00a6fa757da150bee5897b49.zip | |
numeric.c: fix a bug in left shift of negative integer.
`-1 * (1<<63)` causes overflow, but `-1<<63` is a valid value.
Diffstat (limited to 'src/numeric.c')
| -rw-r--r-- | src/numeric.c | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/src/numeric.c b/src/numeric.c index dd06d8115..ded2880ff 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -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; } |
