Skip to content

Commit 8bdec4d

Browse files
authored
Boundschecks in findapproxmax and friends (#393)
* Boundschecks in findapproxmax and friends * use Base.checkbounds
1 parent 154eeba commit 8bdec4d

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

src/ApproxFunBase.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import Base: values, convert, getindex, setindex!, *, +, -, ==, <, <=, >, |, !,
2929
getproperty, findfirst, unsafe_getindex, fld, div,
3030
eachindex, firstindex, lastindex, isreal,
3131
OneTo, Array, Vector, Matrix, view, ones, @propagate_inbounds,
32-
print_array, split, iszero, permutedims, rad2deg, deg2rad
32+
print_array, split, iszero, permutedims, rad2deg, deg2rad, checkbounds
3333

3434
import Base.Broadcast: BroadcastStyle, Broadcasted, AbstractArrayStyle,
3535
broadcastable, DefaultArrayStyle, broadcasted

src/Multivariate/LowRankFun.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ LowRankFun(f::LowRankFun) = LowRankFun(f,ChebyshevInterval(),ChebyshevInterval()
248248
## Utilities
249249

250250
function findapproxmax!(f::Function,X::AbstractMatrix,ptsx::AbstractVector,ptsy::AbstractVector,gridx,gridy)
251+
checkbounds(ptsy, 1:gridy)
252+
checkbounds(ptsx, 1:gridx)
253+
checkbounds(X, 1:gridx, 1:gridy)
251254
for j=1:gridy
252255
ptsyj = ptsy[j]
253256
@simd for k=1:gridx
@@ -268,6 +271,8 @@ function findapproxmax!(A::Fun,B::Fun,X::AbstractMatrix,ptsx::AbstractVector,pts
268271
end
269272

270273
function findcholeskyapproxmax!(f::Function,X::AbstractVector,pts::AbstractVector,grid)
274+
checkbounds(pts, 1:grid)
275+
checkbounds(X, 1:grid)
271276
@simd for k=1:grid
272277
@inbounds X[k]+=f(pts[k],pts[k])
273278
end
@@ -283,6 +288,9 @@ function findcholeskyapproxmax!(A::Fun,B::Fun,X::AbstractVector,pts::AbstractVec
283288
end
284289

285290
function subtractrankone!(A::AbstractVector,B::AbstractVector,X::AbstractMatrix,gridx::Int,gridy::Int)
291+
checkbounds(B, 1:gridy)
292+
checkbounds(A, 1:gridx)
293+
checkbounds(X, 1:gridx, 1:gridy)
286294
for j=1:gridy
287295
@inbounds Bj = B[j]
288296
@simd for k=1:gridx
@@ -292,6 +300,9 @@ function subtractrankone!(A::AbstractVector,B::AbstractVector,X::AbstractMatrix,
292300
end
293301

294302
function subtractrankone!(A::AbstractVector,B::AbstractVector,X::AbstractVector,grid::Int)
303+
checkbounds(A, 1:grid)
304+
checkbounds(B, 1:grid)
305+
checkbounds(X, 1:grid)
295306
@simd for k=1:grid
296307
@inbounds X[k] -= A[k]*B[k]
297308
end

src/Operators/SubOperator.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
Vcheckbounds(A::Operator,kr::Colon) = nothing
2-
3-
@inline function checkbounds(A, inds...)
4-
_checkbounds(A, inds...)::Bool || throw(BoundsError(A,inds))
1+
function checkbounds(A::Operator, inds...)
2+
checkbounds(Bool, A, inds...) || throw(BoundsError(A,inds))
53
nothing
64
end
75

8-
_checkbounds(A::Operator,kr)::Bool =
6+
checkbounds(::Type{Bool}, A::Operator,kr::Colon) = true
7+
8+
checkbounds(::Type{Bool}, A::Operator,kr) =
99
!(maximum(kr) > length(A) || minimum(kr) < 1)
1010

1111

12-
_checkbounds(A::Operator,kr::Union{Colon,InfRanges},jr::Union{Colon,InfRanges})::Bool = true
12+
checkbounds(::Type{Bool}, A::Operator,kr::Union{Colon,InfRanges},jr::Union{Colon,InfRanges}) = true
1313

14-
_checkbounds(A::Operator,kr::Union{Colon,InfRanges},jr)::Bool =
14+
checkbounds(::Type{Bool}, A::Operator,kr::Union{Colon,InfRanges},jr) =
1515
!(maximum(jr) > size(A,2) || minimum(jr) < 1)
1616

17-
_checkbounds(A::Operator,kr,jr::Union{Colon,InfRanges})::Bool =
17+
checkbounds(::Type{Bool}, A::Operator,kr,jr::Union{Colon,InfRanges}) =
1818
!(maximum(kr) > size(A,1) || minimum(kr) < 1 )
1919

20-
function _checkbounds(A::Operator,kr,jr)::Bool
20+
function checkbounds(::Type{Bool}, A::Operator,kr,jr)
2121
(isempty(kr) || isempty(jr)) && return true
2222
(1 <= minimum(kr) <= maximum(kr) <= size(A,1)) &&
2323
(1 <= minimum(jr) <= maximum(jr) <= size(A,2))
2424
end
2525

26-
_checkbounds(A::Operator,K::Block,J::Block)::Bool =
26+
checkbounds(::Type{Bool}, A::Operator,K::Block,J::Block) =
2727
1 first(K.n[1]) length(blocklengths(rangespace(A))) &&
2828
1 first(J.n[1]) length(blocklengths(domainspace(A)))
2929

30-
_checkbounds(A::Operator,K::BlockRange{1},J::BlockRange{1})::Bool =
30+
checkbounds(::Type{Bool}, A::Operator,K::BlockRange{1},J::BlockRange{1}) =
3131
isempty(K) || isempty(J) ||
32-
_checkbounds(A, Block(maximum(K.indices[1])), Block(maximum(J.indices[1])))
32+
checkbounds(Bool, A, Block(maximum(K.indices[1])), Block(maximum(J.indices[1])))
3333

3434

3535

0 commit comments

Comments
 (0)