Skip to content

Commit

Permalink
Fix IntSet overflow with large unsigned ints (#24365)
Browse files Browse the repository at this point in the history
This is already fixed on master through a larger, unrelated change.  This is a very minimal patch to correct the bad behavior.
  • Loading branch information
mbauman authored and ararslan committed Oct 28, 2017
1 parent ac16a87 commit 5478834
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
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

0 comments on commit 5478834

Please sign in to comment.