Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't access uninitialized indices in tril!/triu! for numeric arrays #52528

Merged
merged 9 commits into from
Jan 25, 2024
Prev Previous commit
Next Next commit
propagate inbounds
  • Loading branch information
jishnub committed Jan 15, 2024
commit dbe324e0a8f25b684ee525e1a9442907bd1bcba5
8 changes: 4 additions & 4 deletions stdlib/LinearAlgebra/src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ norm2(x::Union{Array{T},StridedVector{T}}) where {T<:BlasFloat} =
length(x) < NRM2_CUTOFF ? generic_norm2(x) : BLAS.nrm2(x)

# Conservative assessment of types that have zero(T) defined for themselves
_zero(M::AbstractArray{T}, i, j) where {T<:Number} = isconcretetype(T) ? zero(T) : zero(M[i,j])
_zero(M::AbstractArray, i, j) = zero(M[i,j])
@propagate_inbounds _zero(M::AbstractArray{T}, i, j) where {T<:Number} = isconcretetype(T) ? zero(T) : zero(M[i,j])
@propagate_inbounds _zero(M::AbstractArray, i, j) = zero(M[i,j])

"""
triu!(M, k::Integer)
Expand Down Expand Up @@ -143,7 +143,7 @@ function triu!(M::AbstractMatrix, k::Integer)
@inbounds M[i,j] = _zero(M, i,j)
end
end
return M
M
end

triu(M::Matrix, k::Integer) = triu!(copy(M), k)
Expand Down Expand Up @@ -181,7 +181,7 @@ function tril!(M::AbstractMatrix, k::Integer)
@inbounds M[i,j] = _zero(M, i,j)
end
end
return M
M
end

tril(M::Matrix, k::Integer) = tril!(copy(M), k)
Expand Down