Skip to content

Commit b66f63c

Browse files
Ensure length(ipiv)==n before calling LAPACK.getrs! to avoid segfaults (#49602)
1 parent 404bb1f commit b66f63c

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

stdlib/LinearAlgebra/src/lapack.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,9 @@ for (gels, gesv, getrs, getri, elty) in
10101010
if n != size(B, 1)
10111011
throw(DimensionMismatch("B has leading dimension $(size(B,1)), but needs $n"))
10121012
end
1013+
if n != length(ipiv)
1014+
throw(DimensionMismatch("ipiv has length $(length(ipiv)), but needs to be $n"))
1015+
end
10131016
nrhs = size(B, 2)
10141017
info = Ref{BlasInt}()
10151018
ccall((@blasfunc($getrs), libblastrampoline), Cvoid,

stdlib/LinearAlgebra/test/lapack.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,4 +720,13 @@ a = zeros(2,0), zeros(0)
720720
@test LinearAlgebra.LAPACK.geqrf!(a...) === a
721721
@test LinearAlgebra.LAPACK.gerqf!(a...) === a
722722

723+
# Issue #49489: https://github.com/JuliaLang/julia/issues/49489
724+
# Dimension mismatch between A and ipiv causes segfaults
725+
@testset "issue #49489" begin
726+
A = randn(23,23)
727+
b = randn(23)
728+
ipiv = collect(1:20)
729+
@test_throws DimensionMismatch LinearAlgebra.LAPACK.getrs!('N', A, ipiv, b)
730+
end
731+
723732
end # module TestLAPACK

0 commit comments

Comments
 (0)