nagisa / rust_libloading

Bindings around the platform's dynamic library loading primitives with greatly improved memory safety.
https://docs.rs/libloading
ISC License
1.22k stars 100 forks source link

Undefined behavior in `fmt::Debug` impl for `os::windows::Library` #83

Closed Cldfire closed 3 years ago

Cldfire commented 3 years ago

I happened to notice this while glancing around the code for #82. Link to src.

            let mut buf =
                mem::MaybeUninit::<[mem::MaybeUninit::<WCHAR>; 1024]>::uninit().assume_init();

Based on the docs for assume_init, it sounds like this is UB:

Calling this when the content is not yet fully initialized causes immediate undefined behavior.

Cldfire commented 3 years ago

Oh, duh. That's not UB because the contents of the array are actually initialized there, as uninitialized data.

Also realized it's copied from the impl in the stdlib.

Closing, my bad! 😄

nagisa commented 3 years ago

The assume_init is fine becaue the array values are themselves MaybeUninit, yeah.