Skip to content

Commit

Permalink
Avoid out of bounds read in mul! for SymTridiagonal (#27008)
Browse files Browse the repository at this point in the history
* Avoid out of bounds read in mul! for SymTridiagonal

Fixes #26994

* Also handle 0x0 case
  • Loading branch information
andreasnoack committed May 11, 2018
1 parent 1b73dfc commit f90c599
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
13 changes: 9 additions & 4 deletions stdlib/LinearAlgebra/src/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,19 @@ function mul!(C::StridedVecOrMat, S::SymTridiagonal, B::StridedVecOrMat)
throw(DimensionMismatch("second dimension of B, $n, doesn't match second dimension of C, $(size(C,2))"))
end

if m == 0
return C
end

α = S.dv
β = S.ev
@inbounds begin
for j = 1:n
x₀, x₊ = B[1, j], B[2, j]
β₀ = β[1]
C[1, j] = α[1]*x₀ + x₊*β₀
for i = 2:m - 1
x₊ = B[1, j]
x₀ = zero(x₊)
# If m == 1 then β[1] is out of bounds
β₀ = m > 1 ? zero(β[1]) : zero(eltype(β))
for i = 1:m - 1
x₋, x₀, x₊ = x₀, x₊, B[i + 1, j]
β₋, β₀ = β₀, β[i]
C[i, j] = β₋*x₋ + α[i]*x₀ + β₀*x₊
Expand Down
7 changes: 7 additions & 0 deletions stdlib/LinearAlgebra/test/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,11 @@ end
@test Tridiagonal(4:5, 1:3, 1:2) == [1 1 0; 4 2 2; 0 5 3]
end

@testset "Issue #26994 (and the empty case)" begin
T = SymTridiagonal([1.0],[3.0])
x = ones(1)
@test T*x == ones(1)
@test SymTridiagonal(ones(0), ones(0)) * ones(0, 2) == ones(0, 2)
end

end # module TestTridiagonal

0 comments on commit f90c599

Please sign in to comment.