Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions mrbgems/mruby-random/test/random.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,24 @@
# 64-bit mrb_int builds. Such bounds used to overflow `end - begin` in C
# (UndefinedBehaviorSanitizer signed-integer-overflow), found via a
# minimized mruby_fuzzer testcase. On 32-bit mrb_int (or when bigint
# promotes the bounds) these take a different path, so probe the
# integer-range path first and skip if absent.
hi = ((1 << 62) + (1 << 61)) rescue nil
if hi && (Integer === (rand(-hi..hi) rescue nil))
# promotes the bounds) these take a different path, so probe for the bounds
# themselves and skip where they are out of reach. The probe asks what the
# bounds are rather than what `rand` returns for them: the regression answers
# `nil` for the wide range too, so a `rand` guard would read that as "path
# absent" and skip the assertions written to catch it.
# The shift width comes from a variable because `1 << 62` written out is
# constant folded, and the fold fails while this file is compiled on
# MRB_INT32 without bigint, dropping every test in it.
shift = 62
hi = nil
wide = begin
hi = (1 << shift) + (1 << (shift - 1)) # RangeError where mrb_int is 32 bits and bigint is absent
[][hi] # nil for an mrb_int index, RangeError for a big integer
true
rescue RangeError
false
end
if wide
lo = -hi
# reversed wide range -> nil; old code overflowed `end - begin`.
assert_nil(rand(hi..lo))
Expand Down
21 changes: 18 additions & 3 deletions mrbgems/mruby-socket/test/addrinfo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,24 @@
assert('Addrinfo.getaddrinfo rejects out-of-range integer hints') do
# a 64-bit mrb_int that does not fit C int must raise, not be silently
# truncated into a different but still-valid-looking hint (#6960)
skip "needs 64-bit mrb_int" unless (1 << 40).kind_of?(Integer)
assert_raise(RangeError) { Addrinfo.getaddrinfo("localhost", nil, 1 << 40) }
assert_raise(RangeError) { Addrinfo.getaddrinfo("localhost", nil, nil, nil, nil, 1 << 40) }
# The shift width comes from a variable because `1 << 40` written out is
# constant folded, and the fold fails while this file is compiled on
# MRB_INT32 without bigint, dropping every test in it.
shift = 40
big = nil
wide = begin
big = 1 << shift # RangeError where mrb_int is 32 bits and bigint is absent
[][big] # nil for an mrb_int index, RangeError for a big integer
true
rescue RangeError
false
end
# A big integer is not an mrb_int either: the hints keep their unconverted
# type, `Addrinfo.getaddrinfo` leaves the field at its default, and the
# narrowing under test never runs.
skip "needs 64-bit mrb_int" unless wide
assert_raise(RangeError) { Addrinfo.getaddrinfo("localhost", nil, big) }
assert_raise(RangeError) { Addrinfo.getaddrinfo("localhost", nil, nil, nil, nil, big) }
end

assert('Addrinfo.foreach') do
Expand Down
6 changes: 5 additions & 1 deletion test/t/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,11 @@ def [](*args)

# check the overflow to index and length (to be pass without crash)
assert_nothing_raised { "0123456789".bytesplice(8, ~(-1 << 31), "ab") } # for MRB_INT32
assert_nothing_raised { begin; "0123456789".bytesplice(8, ~(-1 << 63), "ab"); rescue ArgumentError, RangeError; end } # for MRB_INT64
# The shift width comes from a variable because `1 << 63` written out is
# constant folded, and the fold fails while this file is compiled on
# MRB_INT32 without bigint, dropping every test in it.
shift = 63
assert_nothing_raised { begin; "0123456789".bytesplice(8, ~(-1 << shift), "ab"); rescue ArgumentError, RangeError; end } # for MRB_INT64

# check the negative index
assert_equal "0ab3456789", "0123456789".bytesplice(-9, 2, "ab")
Expand Down
Loading