Skip to content

Commit 12b8948

Browse files
authored
Fixed segfault when adding OffsetArray and UniformScaling (#38544)
1 parent 8b6a778 commit 12b8948

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

stdlib/LinearAlgebra/src/uniformscaling.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,17 @@ end
215215
function (+)(A::AbstractMatrix, J::UniformScaling)
216216
checksquare(A)
217217
B = copy_oftype(A, Base._return_type(+, Tuple{eltype(A), typeof(J)}))
218-
@inbounds for i in axes(A, 1)
219-
B[i,i] += J
218+
for i in intersect(axes(A,1), axes(A,2))
219+
@inbounds B[i,i] += J
220220
end
221221
return B
222222
end
223223

224224
function (-)(J::UniformScaling, A::AbstractMatrix)
225225
checksquare(A)
226226
B = convert(AbstractMatrix{Base._return_type(+, Tuple{eltype(A), typeof(J)})}, -A)
227-
@inbounds for i in axes(A, 1)
228-
B[i,i] += J
227+
for i in intersect(axes(A,1), axes(A,2))
228+
@inbounds B[i,i] += J
229229
end
230230
return B
231231
end

stdlib/LinearAlgebra/test/uniformscaling.jl

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ using Test, LinearAlgebra, Random, SparseArrays
77
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
88
isdefined(Main, :Quaternions) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "Quaternions.jl"))
99
using .Main.Quaternions
10+
isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "OffsetArrays.jl"))
11+
using .Main.OffsetArrays
1012

1113
Random.seed!(123)
1214

@@ -504,4 +506,12 @@ end
504506
end
505507
end
506508

509+
@testset "offset arrays" begin
510+
A = OffsetArray(zeros(4,4), -1:2, 0:3)
511+
@test sum(I + A) 3.0
512+
@test sum(A + I) 3.0
513+
@test sum(I - A) 3.0
514+
@test sum(A - I) -3.0
515+
end
516+
507517
end # module TestUniformscaling

0 commit comments

Comments
 (0)