Skip to content

Commit

Permalink
Fix Int64 overrun potential in skip_deleted (#31452)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndinsmore authored and JeffBezanson committed Mar 29, 2019
1 parent 8c44566 commit a399780
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -657,18 +657,22 @@ end

function skip_deleted(h::Dict, i)
L = length(h.slots)
@inbounds while i<=L && !isslotfilled(h,i)
i += 1
for i = i:L
@inbounds if isslotfilled(h,i)
return i
end
end
return i
return nothing
end
function skip_deleted_floor!(h::Dict)
idx = skip_deleted(h, h.idxfloor)
h.idxfloor = idx
if idx !== nothing
h.idxfloor = idx
end
idx
end

@propagate_inbounds _iterate(t::Dict{K,V}, i) where {K,V} = i > length(t.vals) ? nothing : (Pair{K,V}(t.keys[i],t.vals[i]), i+1)
@propagate_inbounds _iterate(t::Dict{K,V}, i) where {K,V} = i === nothing ? nothing : (Pair{K,V}(t.keys[i],t.vals[i]), i == typemax(Int) ? nothing : i+1)
@propagate_inbounds function iterate(t::Dict)
_iterate(t, skip_deleted_floor!(t))
end
Expand All @@ -677,11 +681,12 @@ end
isempty(t::Dict) = (t.count == 0)
length(t::Dict) = t.count

@propagate_inbounds function iterate(v::Union{KeySet{<:Any, <:Dict}, ValueIterator{<:Dict}},
i=v.dict.idxfloor)
@propagate_inbounds function Base.iterate(v::T, i::Union{Int,Nothing}=v.dict.idxfloor) where T <: Union{KeySet{<:Any, <:Dict}, ValueIterator{<:Dict}}
i === nothing && return nothing # This is to catch nothing returned when i = typemax
i = skip_deleted(v.dict, i)
i > length(v.dict.vals) && return nothing
(v isa KeySet ? v.dict.keys[i] : v.dict.vals[i], i+1)
i === nothing && return nothing # This is to catch nothing returned by skip_deleted
vals = T <: KeySet ? v.dict.keys : v.dict.vals
(@inbounds vals[i], i == typemax(Int) ? nothing : i+1)
end

filter!(f, d::Dict) = filter_in_one_pass!(f, d)
Expand Down

0 comments on commit a399780

Please sign in to comment.