Skip to content

Commit 10f86ed

Browse files
Try to fix Undefined Behavior in DataLayouts
1 parent 49d7e80 commit 10f86ed

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/DataLayouts/DataLayouts.jl

+17-8
Original file line numberDiff line numberDiff line change
@@ -235,27 +235,29 @@ Base.similar(data::AbstractData{S}) where {S} = similar(data, S)
235235
typesize(eltype(parent(data)), S)
236236
end
237237

238+
@inline _getproperty(data::AbstractData, ::Val{Name}) where {Name} =
239+
_getproperty(data, Val(Name), Name)
240+
238241
@generated function _getproperty(
239242
data::AbstractData{S},
240243
::Val{Name},
244+
name,
241245
) where {S, Name}
242-
errorstring = "Invalid field name $(Name) for type $(S)"
243246
i = findfirst(isequal(Name), fieldnames(S))
244-
if i === nothing
245-
return :(error($errorstring))
246-
end
247247
static_idx = Val{i}()
248-
return :(Base.@_inline_meta; DataLayouts._property_view(data, $static_idx))
248+
return :(
249+
Base.@_inline_meta; DataLayouts._property_view(data, $static_idx, name)
250+
)
249251
end
250252

251253
@inline function Base.getproperty(data::AbstractData{S}, name::Symbol) where {S}
252-
_getproperty(data, Val{name}())
254+
_getproperty(data, Val{name}(), name)
253255
end
254256
@inline function Base.dotgetproperty(
255257
data::AbstractData{S},
256258
name::Symbol,
257259
) where {S}
258-
_getproperty(data, Val{name}())
260+
_getproperty(data, Val{name}(), name)
259261
end
260262

261263
Base.@propagate_inbounds function Base.getproperty(
@@ -275,11 +277,18 @@ Base.@propagate_inbounds function Base.getproperty(
275277
union_all(data){SS, Base.tail(type_params(data))...}(dataview)
276278
end
277279

280+
@noinline _property_view(
281+
data::AbstractData{S},
282+
::Val{Nothing},
283+
name,
284+
) where {S} = error("Invalid field name $name for type $(S)")
285+
278286
# In the past, we've sometimes needed a generated function
279287
# for inference and constant propagation:
280288
Base.@propagate_inbounds @generated function _property_view(
281289
data::AD,
282290
::Val{Idx},
291+
name,
283292
) where {S, Idx, AD <: AbstractData{S}}
284293
SS = fieldtype(S, Idx)
285294
T = eltype(parent_array_type(AD))
@@ -855,7 +864,7 @@ Base.lastindex(data::VF) = length(data)
855864
nlevels(::VF{S, Nv}) where {S, Nv} = Nv
856865

857866
Base.@propagate_inbounds Base.getproperty(data::VF, i::Integer) =
858-
_property_view(data, Val(i))
867+
_property_view(data, Val(i), i)
859868

860869
Base.@propagate_inbounds column(data::VF, i, h) = column(data, i, 1, h)
861870

0 commit comments

Comments
 (0)