-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possible 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::uninitialized
may 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:
SmallVec
isn'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::uninitialized
to create arrays of such types.The ideal solution is to switch from
uninitialized
to the newMaybeUninit
union (rust-lang/rfcs#1892) when it becomes stable.The text was updated successfully, but these errors were encountered: