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 upDropping SmallVecN is unsound #4
Comments
|
CC @pcwalton |
|
I think this used to be sound when Rust had both drop flags and zeroing-on-drop, but that changed in rust-lang/rust#23535. We could probably go back to the old behavior by initializing with rust-lang/rfcs#197 is also relevant. |
|
The replacement for zeroed is |
|
Using |
For example, this segfaults:
SmallVec4<T>contains a[T; 4]field directly, which is initialized innewwithstd::mem::zeroed(). When the vector is dropped, the destructor forTis run for each of the 4Ts, even if there isn’t actually aTthere (i.e. if the vector’s length is less than 4 by the time it is dropped).https://github.com/bluss/arrayvec works around this issue by having (simplified):
with a destructor that resets to
Droppedbefore the recursive destructors are run implicitly.In
SmallVecN, the second variant could instead contain the pointer and capacity for a spilled vector (reset to null/zero during destruction).