diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-07-06 10:08:42 -0700 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-07-06 10:08:42 -0700 |
| commit | 5f74721e02c3a58d10915afe973917ac6c62ccef (patch) | |
| tree | d82c85e504453c8d24e8ee9341c8125ac71058e1 | |
| parent | bccf1259f528e1605308b9cf005ad4f10897e592 (diff) | |
| parent | 9a5e78ae6771c272352d6b59bef264ccac2150ec (diff) | |
| download | mruby-5f74721e02c3a58d10915afe973917ac6c62ccef.tar.gz mruby-5f74721e02c3a58d10915afe973917ac6c62ccef.zip | |
Merge pull request #1348 from carsonmcdonald/shiftwarningfix
Fix signed/unsigned warning in numeric shift
| -rw-r--r-- | src/numeric.c | 4 | ||||
| -rw-r--r-- | test/t/integer.rb | 19 |
2 files changed, 21 insertions, 2 deletions
diff --git a/src/numeric.c b/src/numeric.c index 6cd82d8b6..e1543c089 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -998,7 +998,7 @@ fix_xor(mrb_state *mrb, mrb_value x) #define NUMERIC_SHIFT_WIDTH_MAX (sizeof(mrb_int)*CHAR_BIT-1) static mrb_value -lshift(mrb_state *mrb, mrb_int val, int width) +lshift(mrb_state *mrb, mrb_int val, size_t width) { if (width > NUMERIC_SHIFT_WIDTH_MAX) { mrb_raisef(mrb, E_RANGE_ERROR, "width(%S) > (%S:sizeof(mrb_int)*CHAR_BIT-1)", @@ -1010,7 +1010,7 @@ lshift(mrb_state *mrb, mrb_int val, int width) } static mrb_value -rshift(mrb_int val, int width) +rshift(mrb_int val, size_t width) { if (width >= NUMERIC_SHIFT_WIDTH_MAX) { if (val < 0) { diff --git a/test/t/integer.rb b/test/t/integer.rb index 46011f94a..9b19216d9 100644 --- a/test/t/integer.rb +++ b/test/t/integer.rb @@ -104,6 +104,14 @@ assert('Integer#<<', '15.2.8.3.12') do # 00010111 (23) # = 00101110 (46) assert_equal 23 << 1, 46 + + # Left Shift by a negative is Right Shift + assert_equal 46 << -1, 23 + + # Raise when shift is too large + assert_raise(RangeError) do + 2 << 128 + end end assert('Integer#>>', '15.2.8.3.13') do @@ -111,6 +119,17 @@ assert('Integer#>>', '15.2.8.3.13') do # 00101110 (46) # = 00010111 (23) assert_equal 46 >> 1, 23 + + # Right Shift by a negative is Left Shift + assert_equal 23 >> -1, 46 + + # Don't raise on large Right Shift + assert_equal 23 >> 128, 0 + + # Raise when shift is too large + assert_raise(RangeError) do + 2 >> -128 + end end assert('Integer#ceil', '15.2.8.3.14') do |
