From f7b39e760d5b9fc1912c71f4e6e4ca5e99992dc3 Mon Sep 17 00:00:00 2001 From: Aravindh Krishnamoorthy Date: Wed, 3 May 2023 09:19:12 +0200 Subject: [PATCH] Ensure length(ipiv)==n before calling LAPACK.getrs! to avoid segfaults (#49602) (cherry picked from commit b66f63c5add314bc345e2a7e1ad775d52ad58065) --- stdlib/LinearAlgebra/src/lapack.jl | 3 +++ stdlib/LinearAlgebra/test/lapack.jl | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/stdlib/LinearAlgebra/src/lapack.jl b/stdlib/LinearAlgebra/src/lapack.jl index 9edaf77440750..bef018307fcf7 100644 --- a/stdlib/LinearAlgebra/src/lapack.jl +++ b/stdlib/LinearAlgebra/src/lapack.jl @@ -1010,6 +1010,9 @@ for (gels, gesv, getrs, getri, elty) in if n != size(B, 1) throw(DimensionMismatch("B has leading dimension $(size(B,1)), but needs $n")) end + if n != length(ipiv) + throw(DimensionMismatch("ipiv has length $(length(ipiv)), but needs to be $n")) + end nrhs = size(B, 2) info = Ref{BlasInt}() ccall((@blasfunc($getrs), libblastrampoline), Cvoid, diff --git a/stdlib/LinearAlgebra/test/lapack.jl b/stdlib/LinearAlgebra/test/lapack.jl index e0e75f0a88413..f8baceebaff52 100644 --- a/stdlib/LinearAlgebra/test/lapack.jl +++ b/stdlib/LinearAlgebra/test/lapack.jl @@ -709,4 +709,13 @@ a = zeros(2,0), zeros(0) @test LinearAlgebra.LAPACK.geqrf!(a...) === a @test LinearAlgebra.LAPACK.gerqf!(a...) === a +# Issue #49489: https://github.com/JuliaLang/julia/issues/49489 +# Dimension mismatch between A and ipiv causes segfaults +@testset "issue #49489" begin + A = randn(23,23) + b = randn(23) + ipiv = collect(1:20) + @test_throws DimensionMismatch LinearAlgebra.LAPACK.getrs!('N', A, ipiv, b) +end + end # module TestLAPACK