From a7f798a48bdc89af944709c95e450a484a12dff0 Mon Sep 17 00:00:00 2001 From: Matt Bauman Date: Fri, 27 Oct 2017 14:30:47 -0500 Subject: [PATCH 1/2] [Release-0.6] Fix IntSet overflow with large unsigned ints This is already fixed on master through a larger, unrelated change. This is a very minimal patch to correct the bad behavior. --- base/intset.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/intset.jl b/base/intset.jl index 1e45f8076c903..956d12498a253 100644 --- a/base/intset.jl +++ b/base/intset.jl @@ -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 From e6390dc67659e7ba09f7a5c77aeaaaa3045313a0 Mon Sep 17 00:00:00 2001 From: Matt Bauman Date: Fri, 27 Oct 2017 14:34:17 -0500 Subject: [PATCH 2/2] add tests --- test/intset.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/intset.jl b/test/intset.jl index 6d0afdaaba472..3fe2ffb413ee9 100644 --- a/test/intset.jl +++ b/test/intset.jl @@ -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