Skip to content

Commit 70d1d95

Browse files
committed
made iterate method safe
1 parent 5be83f9 commit 70d1d95

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

base/subarray.jl

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,28 @@ find_extended_inds(::ScalarIndex, I...) = (@inline; find_extended_inds(I...))
433433
find_extended_inds(i1, I...) = (@inline; (i1, find_extended_inds(I...)...))
434434
find_extended_inds() = ()
435435

436-
function iterate(V::FastContiguousSubArray,
437-
state = (V.offset1+firstindex(V), V.offset1+lastindex(V)))
436+
function isvalid_index(p, i, l)
437+
if firstindex(p) == 1
438+
(i-1)%UInt < l%UInt <= lastindex(p)%UInt
439+
else
440+
firstindex(p) <= i <= l <= lastindex(p)
441+
end
442+
end
443+
444+
function Base.iterate(v::Base.FastContiguousSubArray,
445+
state = v.offset1 .+ (firstindex(v), lastindex(v)))
446+
p = parent(v)
447+
i, l = state
448+
isvalid_index(p, i, l) ? (@inbounds p[i], (i+1, l)) : nothing
449+
end
450+
451+
stride1(v::SubArray{T,N,P}) where {T,N,P} = P <: StridedArray ? stride(v, 1) : v.stride1
452+
453+
function Base.iterate(v::Base.FastSubArray,
454+
state = v.offset1 .+ stride1(v) .* (firstindex(v), lastindex(v)))
455+
p = parent(v)
438456
i, l = state
439-
@inbounds i-1 < l ? (V.parent[i], (i+1, l)) : nothing
457+
isvalid_index(p, i, l) ? (@inbounds p[i], (i+stride1(v), l)) : nothing
440458
end
441459

442460
function unsafe_convert(::Type{Ptr{T}}, V::SubArray{T,N,P,<:Tuple{Vararg{RangeIndex}}}) where {T,N,P}

0 commit comments

Comments
 (0)