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

UB with std::mem::zeroed #19

Open
nico-abram opened this issue Nov 22, 2020 · 3 comments
Open

UB with std::mem::zeroed #19

nico-abram opened this issue Nov 22, 2020 · 3 comments

Comments

@nico-abram
Copy link

nico-abram commented Nov 22, 2020

I think this line

let mut callbacks: vorbisfile_sys::ov_callbacks = unsafe { std::mem::zeroed() };
is invoking undefined behaviour

zero is a trap/niche value for fn function pointers (afaik even extern "C" ones, and even for repr(C)). ov_callbacks is a struct of function pointers https://github.com/tomaka/vorbisfile-sys/blob/master/src/lib.rs#L7-L14

The line in queston is initializing an instance of said struct with mem::zeroed, which as far as I understand, is insta-UB

I originally found this because of a runtime panic from rustc 1.49.0-nightly (ffa2e7ae8 2020-10-24) while running the lewton benchmarks
imagen

I think the fix would be making the fields in the struct in vorbisfile Option's, and then making the appropiate changes in vorbis-rs

I opened tomaka/vorbisfile-sys#2 to begin that process

@roderickvd
Copy link

Besides the route you have been taking, a quick fix may be to change std::mem::zeroed() to std::mem::uninitialized(). This still is not considered as safe as it should be, better Rust parlance seems to be like so: https://doc.rust-lang.org/std/mem/union.MaybeUninit.html

@nico-abram
Copy link
Author

Changing it to uninitialized might help get rid of the panic, but it would still be UB AIUI if the struct has any niche values (Like it currently does with 0), and whether or not it is UB if all bit patterns are valid is still in discussion iirc. As you say, MaybeUninit is the preferred alternative

@Johannesd3
Copy link

Please take a look at librespot-org/librespot-tremor#2 which was basically the same issue. As a matter of fact, it seems as if the code of that crate was "stolen" from this repository (?).

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

3 participants