Skip to content

Commit 5ec4a56

Browse files
jd-fosterlazarusA
authored andcommitted
Avoid StackOverflowError of generic norm on AbstractChar (JuliaLang#54122)
1 parent 5131c75 commit 5ec4a56

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

stdlib/LinearAlgebra/src/generic.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,9 @@ true
668668
"""
669669
Base.@constprop :aggressive function norm(itr, p::Real=2)
670670
isempty(itr) && return float(norm(zero(eltype(itr))))
671+
v, s = iterate(itr)
672+
!isnothing(s) && !ismissing(v) && v == itr && throw(ArgumentError(
673+
"cannot evaluate norm recursively if the type of the initial element is identical to that of the container"))
671674
if p == 2
672675
return norm2(itr)
673676
elseif p == 1

stdlib/LinearAlgebra/test/generic.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,13 @@ end
571571
@test_broken ismissing(norm(x, 0))
572572
end
573573

574+
@testset "avoid stackoverflow of norm on AbstractChar" begin
575+
@test_throws ArgumentError norm('a')
576+
@test_throws ArgumentError norm(['a', 'b'])
577+
@test_throws ArgumentError norm("s")
578+
@test_throws ArgumentError norm(["s", "t"])
579+
end
580+
574581
@testset "peakflops" begin
575582
@test LinearAlgebra.peakflops(1024, eltype=Float32, ntrials=2) > 0
576583
end

0 commit comments

Comments
 (0)