Skip to content

Commit ffc8ee3

Browse files
Try to fix Undefined Behavior in DataLayouts
1 parent 0b6aa34 commit ffc8ee3

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/DataLayouts/DataLayouts.jl

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

238+
@inline _getproperty(
239+
data::AbstractData,
240+
::Val{Name},
241+
) where {Name} = _getproperty(data, Val(Name), Name)
242+
238243
@generated function _getproperty(
239244
data::AbstractData{S},
240245
::Val{Name},
246+
name,
241247
) where {S, Name}
242-
errorstring = "Invalid field name $(Name) for type $(S)"
243248
i = findfirst(isequal(Name), fieldnames(S))
244-
if i === nothing
245-
return :(error($errorstring))
246-
end
247249
static_idx = Val{i}()
248-
return :(Base.@_inline_meta; DataLayouts._property_view(data, $static_idx))
250+
return :(
251+
Base.@_inline_meta; DataLayouts._property_view(data, $static_idx, name)
252+
)
249253
end
250254

251255
@inline function Base.getproperty(data::AbstractData{S}, name::Symbol) where {S}
252-
_getproperty(data, Val{name}())
256+
_getproperty(data, Val{name}(), name)
253257
end
254258
@inline function Base.dotgetproperty(
255259
data::AbstractData{S},
256260
name::Symbol,
257261
) where {S}
258-
_getproperty(data, Val{name}())
262+
_getproperty(data, Val{name}(), name)
259263
end
260264

261265
Base.@propagate_inbounds function Base.getproperty(
@@ -275,11 +279,18 @@ Base.@propagate_inbounds function Base.getproperty(
275279
union_all(data){SS, Base.tail(type_params(data))...}(dataview)
276280
end
277281

282+
@noinline _property_view(
283+
data::AbstractData{S},
284+
::Val{Nothing},
285+
name,
286+
) where {S} = error("Invalid field name $name for type $(S)")
287+
278288
# In the past, we've sometimes needed a generated function
279289
# for inference and constant propagation:
280290
Base.@propagate_inbounds @generated function _property_view(
281291
data::AD,
282292
::Val{Idx},
293+
name,
283294
) where {S, Idx, AD <: AbstractData{S}}
284295
SS = fieldtype(S, Idx)
285296
T = eltype(parent_array_type(AD))
@@ -674,7 +685,7 @@ Base.lastindex(data::VF) = length(data)
674685
nlevels(::VF{S, Nv}) where {S, Nv} = Nv
675686

676687
Base.@propagate_inbounds Base.getproperty(data::VF, i::Integer) =
677-
_property_view(data, Val(i))
688+
_property_view(data, Val(i), i)
678689

679690
Base.@propagate_inbounds column(data::VF, i, h) = column(data, i, 1, h)
680691

0 commit comments

Comments
 (0)