Skip to content

Commit a004142

Browse files
jishnubKristofferC
authored andcommitted
Fix segfault in Diagonal * OffsetMatrix (#45548)
* diagonal*offset matrix should throw (cherry picked from commit 172bddc)
1 parent d6bbed6 commit a004142

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

stdlib/LinearAlgebra/src/diagonal.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ function *(D::Diagonal, transA::Transpose{<:Any,<:AbstractMatrix})
285285
end
286286

287287
@inline function __muldiag!(out, D::Diagonal, B, alpha, beta)
288+
require_one_based_indexing(B)
288289
require_one_based_indexing(out)
289290
if iszero(alpha)
290291
_rmul_or_fill!(out, beta)
@@ -306,6 +307,7 @@ end
306307
return out
307308
end
308309
@inline function __muldiag!(out, A, D::Diagonal, alpha, beta)
310+
require_one_based_indexing(A)
309311
require_one_based_indexing(out)
310312
if iszero(alpha)
311313
_rmul_or_fill!(out, beta)

stdlib/LinearAlgebra/test/diagonal.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
99
isdefined(Main, :Furlongs) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "Furlongs.jl"))
1010
using .Main.Furlongs
1111

12+
isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "OffsetArrays.jl"))
13+
using .Main.OffsetArrays
14+
1215
n=12 #Size of matrix problem to test
1316
Random.seed!(1)
1417

@@ -785,6 +788,16 @@ end
785788
@test_throws DimensionMismatch lmul!(Diagonal([1]), [1,2,3]) # nearby
786789
end
787790

791+
@testset "Multiplication of a Diagonal with an OffsetArray" begin
792+
# Offset indices should throw
793+
D = Diagonal(1:4)
794+
A = OffsetArray(rand(4,4), 2, 2)
795+
@test_throws ArgumentError D * A
796+
@test_throws ArgumentError A * D
797+
@test_throws ArgumentError mul!(similar(A, size(A)), A, D)
798+
@test_throws ArgumentError mul!(similar(A, size(A)), D, A)
799+
end
800+
788801
@testset "Triangular division by Diagonal #27989" begin
789802
K = 5
790803
for elty in (Float32, Float64, ComplexF32, ComplexF64)

0 commit comments

Comments
 (0)