Skip to content

Commit 3ce3b20

Browse files
authored
Avoid possible stackoverflow in FieldArray's constructor (#1090)
* Avoid stackoverflow in FieldArray's constructor * Add type info of inputs.
1 parent 1686280 commit 3ce3b20

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StaticArrays"
22
uuid = "90137ffa-7385-5640-81b9-e52037218182"
3-
version = "1.5.6"
3+
version = "1.5.7"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/FieldArray.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
function construct_type(::Type{FA}, x) where {FA <: FieldArray}
55
has_size(FA) || error("$FA has no static size!")
66
length_match_size(FA, x)
7-
return adapt_eltype(FA, x)
7+
FA′ = adapt_eltype(FA, x)
8+
FA′ === FA && x isa Args && _missing_fa_constructor(FA, typeof(x.args))
9+
return FA′
10+
end
11+
@noinline function _missing_fa_constructor(@nospecialize(FA), @nospecialize(AT))
12+
Ts = join(("::$T" for T in fieldtypes(AT)), ", ")
13+
error("The constructor for $FA($Ts) is missing!")
814
end
915

1016
@propagate_inbounds getindex(a::FieldArray, i::Int) = getfield(a, i)

test/FieldVector.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,14 @@
130130
@test @inferred(similar_type(FVT{Float64}, Size(3))) == SVector{3,Float64}
131131
@test @inferred(similar_type(FVT{Float64}, Float32, Size(3))) == SVector{3,Float32}
132132
end
133+
134+
@testset "FieldVector with constructor missing" begin
135+
struct Position1088{T} <: FieldVector{3, T}
136+
x::T
137+
y::T
138+
z::T
139+
Position1088(x::T, y::T, z::T) where {T} = new{T}(x, y, z)
140+
end
141+
@test_throws ErrorException("The constructor for Position1088{Float64}(::Float64, ::Float64, ::Float64) is missing!") Position1088((1.,2.,3.))
142+
end
133143
end

0 commit comments

Comments
 (0)