Skip to content

Commit ee1034e

Browse files
authored
Avoid stackoverflow in defaultgetindex (#489)
* Avoid stackoverflow in defaultgetindex * test for defaultgetindexing stackoverflow * types in error message
1 parent a48f495 commit ee1034e

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/Operators/Operator.jl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,25 @@ index_ndims(inds...) = Val(sum(index_ndim, inds))
260260
select_vectorinds(a, b...) = a
261261
select_vectorinds(a::Integer, b...) = select_vectorinds(b...)
262262

263-
combine_inds(inds::Tuple, k) = (k, combine_inds(inds[2:end], k)...)
264-
combine_inds(inds::Tuple{Integer,Vararg}, k) = (inds[1], combine_inds(inds[2:end], k)...)
265-
combine_inds(::Tuple{}, k) = ()
263+
replace_vector_by_scalar(inds::Tuple, k) = (k, replace_vector_by_scalar(inds[2:end], k)...)
264+
replace_vector_by_scalar(inds::Tuple{Integer,Vararg}, k) = (inds[1], replace_vector_by_scalar(inds[2:end], k)...)
265+
replace_vector_by_scalar(::Tuple{}, k) = ()
266266

267267
defaultgetindex(B::Operator, kj...) = defaultgetindex(B, index_ndims(kj...), kj...)
268268

269-
defaultgetindex(op::Operator, ::Val{1}, inds...) =
270-
eltype(op)[op[combine_inds(inds, k)...] for k in select_vectorinds(inds...)]
269+
function defaultgetindex(op::Operator, ::Val{1}, inds...)
270+
indsvec = select_vectorinds(inds...)
271+
# avoid stack-overflow if iterating over indsvec returns indsvec
272+
# e.g. Blocks
273+
k = first(indsvec)
274+
indsscal = replace_vector_by_scalar(inds, k)
275+
if typeof(indsscal) == typeof(inds)
276+
T = typeof(op)
277+
throw(ArgumentError("please implement "*
278+
"getindex(::$T, $(join(string.("::", typeof.(inds)), ",")))"))
279+
end
280+
eltype(op)[op[replace_vector_by_scalar(inds, k)...] for k in indsvec]
281+
end
271282

272283
function defaultgetindex(B::Operator, ::Val{2}, inds...)
273284
S = view(B,inds...)

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,10 @@ end
482482
v = @inferred mul_coefficients(T, Float64[1:4;])
483483
@test v == Float64[1:4;].^2
484484
end
485+
@testset "Evaluation" begin
486+
E = Evaluation(PointSpace(1:3), 1)
487+
@test_throws ArgumentError E[Block(1)]
488+
end
485489
end
486490

487491
@testset "RowVector" begin

0 commit comments

Comments
 (0)