Skip to content

Commit 5f74721

Browse files
committed
Merge pull request mruby#1348 from carsonmcdonald/shiftwarningfix
Fix signed/unsigned warning in numeric shift
2 parents bccf125 + 9a5e78a commit 5f74721

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

src/numeric.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ fix_xor(mrb_state *mrb, mrb_value x)
998998
#define NUMERIC_SHIFT_WIDTH_MAX (sizeof(mrb_int)*CHAR_BIT-1)
999999

10001000
static mrb_value
1001-
lshift(mrb_state *mrb, mrb_int val, int width)
1001+
lshift(mrb_state *mrb, mrb_int val, size_t width)
10021002
{
10031003
if (width > NUMERIC_SHIFT_WIDTH_MAX) {
10041004
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)
10101010
}
10111011

10121012
static mrb_value
1013-
rshift(mrb_int val, int width)
1013+
rshift(mrb_int val, size_t width)
10141014
{
10151015
if (width >= NUMERIC_SHIFT_WIDTH_MAX) {
10161016
if (val < 0) {

test/t/integer.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,32 @@
104104
# 00010111 (23)
105105
# = 00101110 (46)
106106
assert_equal 23 << 1, 46
107+
108+
# Left Shift by a negative is Right Shift
109+
assert_equal 46 << -1, 23
110+
111+
# Raise when shift is too large
112+
assert_raise(RangeError) do
113+
2 << 128
114+
end
107115
end
108116

109117
assert('Integer#>>', '15.2.8.3.13') do
110118
# Right Shift by one
111119
# 00101110 (46)
112120
# = 00010111 (23)
113121
assert_equal 46 >> 1, 23
122+
123+
# Right Shift by a negative is Left Shift
124+
assert_equal 23 >> -1, 46
125+
126+
# Don't raise on large Right Shift
127+
assert_equal 23 >> 128, 0
128+
129+
# Raise when shift is too large
130+
assert_raise(RangeError) do
131+
2 >> -128
132+
end
114133
end
115134

116135
assert('Integer#ceil', '15.2.8.3.14') do

0 commit comments

Comments
 (0)