Skip to content
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

Dropping SmallVecN is unsound #4

Closed
SimonSapin opened this issue May 24, 2015 · 4 comments
Closed

Dropping SmallVecN is unsound #4

SimonSapin opened this issue May 24, 2015 · 4 comments

Comments

@SimonSapin
Copy link
Member

For example, this segfaults:

SmallVec4::<Box<u32>>::new();

SmallVec4<T> contains a [T; 4] field directly, which is initialized in new with std::mem::zeroed(). When the vector is dropped, the destructor for T is run for each of the 4 Ts, even if there isn’t actually a T there (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):

enum Array<T> {
    Alive([T, 4]),
    Dropped,
}

with a destructor that resets to Dropped before 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).

@SimonSapin
Copy link
Member Author

CC @pcwalton

@SimonSapin
Copy link
Member Author

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 std::mem::POST_DROP_U8 instead of zeroes, but I think that’s not a good idea since drop flags are probably going away entirely.

rust-lang/rfcs#197 is also relevant.

@bluss
Copy link
Contributor

bluss commented May 26, 2015

The replacement for zeroed is mem::dropped with filling drop.

@SimonSapin
Copy link
Member Author

Using dropped instead of zeroed could be a short-term fix, but it requires #![feature(filling_drop)] whereas the library currently runs (although brokenly) on Rust stable. I’d prefer using NoDrop from arrayvec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants