Skip to content

Commit 213b887

Browse files
authored
Fix boundscheck in suboperator (#291)
* Fix boundscheck in suboperator * Empty indices
1 parent c02d5e9 commit 213b887

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

src/Operators/SubOperator.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ _checkbounds(A::Operator,kr::Union{Colon,InfRanges},jr)::Bool =
1717
_checkbounds(A::Operator,kr,jr::Union{Colon,InfRanges})::Bool =
1818
!(maximum(kr) > size(A,1) || minimum(kr) < 1 )
1919

20-
_checkbounds(A::Operator,kr,jr)::Bool =
21-
!(!isempty(kr) && (maximum(kr) > size(A,1) || minimum(kr) < 1)) ||
22-
(!isempty(jr) && (maximum(jr) > size(A,2) || minimum(jr) < 1))
23-
20+
function _checkbounds(A::Operator,kr,jr)::Bool
21+
(isempty(kr) || isempty(jr)) && return true
22+
(1 <= minimum(kr) <= maximum(kr) <= size(A,1)) &&
23+
(1 <= minimum(jr) <= maximum(jr) <= size(A,2))
24+
end
2425

2526
_checkbounds(A::Operator,K::Block,J::Block)::Bool =
2627
1 first(K.n[1]) length(blocklengths(rangespace(A))) &&

src/Operators/general/algebra.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ end
387387
_rettype(::Type{BandedMatrix{T}}) where {T} = BandedMatrix{T,Matrix{T},Base.OneTo{Int}}
388388
_rettype(T) = T
389389
for TYP in (:Matrix, :BandedMatrix, :RaggedMatrix)
390-
@eval function $TYP(V::SubOperator{T,TO,Tuple{UnitRange{Int},UnitRange{Int}}}) where {T,TO<:TimesOperator}
390+
@eval function $TYP(V::SubOperator{T,<:TimesOperator,NTuple{2,UnitRange{Int}}}) where {T}
391391
P = parent(V)
392392

393393
if isbanded(P)
@@ -407,7 +407,7 @@ for TYP in (:Matrix, :BandedMatrix, :RaggedMatrix)
407407

408408
if maximum(kr) > size(P,1) || maximum(jr) > size(P,2) ||
409409
minimum(kr) < 1 || minimum(jr) < 1
410-
throw(BoundsError())
410+
throw(BoundsError(P, (kr,jr)))
411411
end
412412

413413
@assert length(P.ops) 2
@@ -458,7 +458,7 @@ for TYP in (:Matrix, :BandedMatrix, :RaggedMatrix)
458458
end
459459

460460
for TYP in (:BlockBandedMatrix, :BandedBlockBandedMatrix)
461-
@eval function $TYP(V::SubOperator{T,TO,Tuple{BlockRange1,BlockRange1}}) where {T,TO<:TimesOperator}
461+
@eval function $TYP(V::SubOperator{T,<:TimesOperator,Tuple{BlockRange1,BlockRange1}}) where {T}
462462
P = parent(V)
463463
KR,JR = parentindices(V)
464464

@@ -469,7 +469,7 @@ for TYP in (:BlockBandedMatrix, :BandedBlockBandedMatrix)
469469

470470
if Int(maximum(KR)) > blocksize(P,1) || Int(maximum(JR)) > blocksize(P,2) ||
471471
Int(minimum(KR)) < 1 || Int(minimum(JR)) < 1
472-
throw(BoundsError())
472+
throw(BoundsError(P, (KR,JR)))
473473
end
474474

475475

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using ApproxFunBase, LinearAlgebra, Random, Test
22
using ApproxFunBase:
33
using Aqua
44
using SpecialFunctions
5+
using BandedMatrices
56

67
@testset "Project quality" begin
78
Aqua.test_all(ApproxFunBase, ambiguities=false)
@@ -265,6 +266,11 @@ end
265266
@test size(V) == (1,)
266267
@test all(==(1), V)
267268
end
269+
A = Fun(PointSpace(1:4))
270+
B = Multiplication(A, space(A))
271+
C = B * B
272+
@test_throws BoundsError @view C[1:2, 0:2]
273+
@test_throws BoundsError @view C[1:10, 1:2]
268274
end
269275
end
270276
@testset "conversion to a matrix" begin

0 commit comments

Comments
 (0)