Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upPossible UB from use of uninitialized `[&T; N]` #126
Comments
|
See also rust-lang/rust#54542. |
|
I can't make the test fail, but I think we have a problem hiding in here, as long as an optional smallvec of references is the same size as the plain type: |
|
SmallVec contains an enum, so I think the enum layout optimization is using spare bits from that enum's tag to store the Option discriminant. If this is true then we're not seeing incorrect behavior in practice yet. |
|
I recently looked into the use of |
Creating an array with
mem::uninitializedmay be UB if the array's elements contain non-null types (like&T) or uninhabited types (like!) or other types that have invalid values.This can result in undefined behavior even if you never read the elements directly while they are uninitialized. For example, this program prints "true" if optimizations are enabled, when built with current rustc:
SmallVecisn't eligible for null pointer optimization, so it isn't affected by this. However, it's possible that a future Rust compiler could add other optimizations that break if SmallVec usesmem::uninitializedto create arrays of such types.The ideal solution is to switch from
uninitializedto the newMaybeUninitunion (rust-lang/rfcs#1892) when it becomes stable.