- Notifications
You must be signed in to change notification settings - Fork163
Description
Creating an array withmem::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:
fnmain(){let x:Option<[&u8;1]> =unsafe{Some(std::mem::uninitialized())};println!("{}", x.is_none());}
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 fromuninitialized to the newMaybeUninit union (rust-lang/rfcs#1892) when it becomes stable.