Skip to content

Commit

Permalink
Fix segfault in Diagonal * OffsetMatrix (#45548)
Browse files Browse the repository at this point in the history
* diagonal*offset matrix should throw
  • Loading branch information
jishnub authored Jun 2, 2022
1 parent 5ffdc5c commit 172bddc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions stdlib/LinearAlgebra/src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ function *(D::Diagonal, transA::Transpose{<:Any,<:AbstractMatrix})
end

@inline function __muldiag!(out, D::Diagonal, B, alpha, beta)
require_one_based_indexing(B)
require_one_based_indexing(out)
if iszero(alpha)
_rmul_or_fill!(out, beta)
Expand All @@ -306,6 +307,7 @@ end
return out
end
@inline function __muldiag!(out, A, D::Diagonal, alpha, beta)
require_one_based_indexing(A)
require_one_based_indexing(out)
if iszero(alpha)
_rmul_or_fill!(out, beta)
Expand Down
13 changes: 13 additions & 0 deletions stdlib/LinearAlgebra/test/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
isdefined(Main, :Furlongs) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "Furlongs.jl"))
using .Main.Furlongs

isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "OffsetArrays.jl"))
using .Main.OffsetArrays

n=12 #Size of matrix problem to test
Random.seed!(1)

Expand Down Expand Up @@ -785,6 +788,16 @@ end
@test_throws DimensionMismatch lmul!(Diagonal([1]), [1,2,3]) # nearby
end

@testset "Multiplication of a Diagonal with an OffsetArray" begin
# Offset indices should throw
D = Diagonal(1:4)
A = OffsetArray(rand(4,4), 2, 2)
@test_throws ArgumentError D * A
@test_throws ArgumentError A * D
@test_throws ArgumentError mul!(similar(A, size(A)), A, D)
@test_throws ArgumentError mul!(similar(A, size(A)), D, A)
end

@testset "Triangular division by Diagonal #27989" begin
K = 5
for elty in (Float32, Float64, ComplexF32, ComplexF64)
Expand Down

0 comments on commit 172bddc

Please sign in to comment.