Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Release-0.6] Fix IntSet overflow with large unsigned ints #24365

Merged
merged 2 commits into from
Oct 28, 2017
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
2 changes: 1 addition & 1 deletion base/intset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ sizehint!(s::IntSet, n::Integer) = (_resize0!(s.bits, max(n, length(s.bits))); s
@inline function _setint!(s::IntSet, idx::Integer, b::Bool)
if idx > length(s.bits)
b || return s # setting a bit to zero outside the set's bits is a no-op
newlen = idx + idx>>1 # This operation may overflow; we want saturation
newlen = Int(idx) + Int(idx)>>1 # This operation may overflow; we want saturation
_resize0!(s.bits, ifelse(newlen<0, typemax(Int), newlen))
end
@inbounds s.bits[idx] = b
Expand Down
7 changes: 7 additions & 0 deletions test/intset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,10 @@ end
@test pop!(s, 100, 0) === 0
@test pop!(s, 99, 0) === 99
end

@testset "unsigned overflow" begin
@test IntSet(UInt8(2^8-1)) == IntSet(2^8-1)
@test [x for x in IntSet(UInt8(2^8-1))] == [UInt8(2^8-1)]
@test IntSet(UInt16(2^16-1)) == IntSet(2^16-1)
@test [x for x in IntSet(UInt16(2^16-1))] == [UInt16(2^16-1)]
end