benfred / remoteprocess

Cross platform process information in Rust
MIT License
56 stars 29 forks source link

Undefined Behavior from use of MaybeUninit assume_init #86

Closed LunNova closed 1 month ago

LunNova commented 5 months ago

I think the usage of assume_init on an uninit MaybeUninit is incorrect.

https://github.com/benfred/remoteprocess/blob/3e94f997345565e3dfe36e1dc206abb557c13e67/src/linux/libunwind/mod.rs#L109-L112

Rust Docs: MaybeUninit Initialization Invariant

let b: bool = unsafe { MaybeUninit::uninit().assume_init() }; // undefined behavior! ⚠️

Moreover, uninitialized memory is special in that it does not have a fixed value (“fixed” meaning “it won’t change without being written to”). Reading the same uninitialized byte multiple times can give different results. This makes it undefined behavior to have uninitialized data in a variable even if that variable has an integer type, which otherwise can hold any fixed bit pattern.

let x: i32 = unsafe { MaybeUninit::uninit().assume_init() }; // undefined behavior! ⚠️