Skip to content

[BUG] Safety comments for MaybeUninit::assume_init calls are wrong, calls are UB #95

@saethlin

Description

@saethlin

This code executes UB:

let buffer: mem::MaybeUninit<[u8; SIZE]> = mem::MaybeUninit::uninit();
// SAFETY: safe, since we never read bytes that weren't written.
let mut buffer = unsafe { buffer.assume_init() };

The docs for MaybeUninit::uninit do not have an exception for this use case. This code is UB, because the MaybeUninit is not initialized.

The safety comment is also technically wrong; the value is read by the assignment and return from MaybeUninit::assume_init.

This problem is reliably reported by running cargo +nightly miri test --all-features.


The existing MaybeUninit APIs are not exactly elegant, but I think they can be slotted in pretty neatly with the existing abstractions you have here. I can take a shot at fixing this in the coming days/weeks.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinghigh priorityHigh priority

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions