MaybeUninit::uninit().assume_init() reads uninitialised memory
ID |
rust.correctness.maybeuninit_uninit_assume_init |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
high |
Remediation Effort |
medium |
Resource |
Undefined Behaviour |
Language |
Rust |
Description
Calling assume_init() straight after uninit() asserts that memory is initialised when nothing has written to it, which is undefined behaviour for every type that has invalid bit patterns. Write the value first and only then call assume_init().
Rationale
Calling assume_init() straight after uninit() asserts that memory is initialised when nothing has written to it, which is undefined behaviour for every type that has invalid bit patterns. Write the value first and only then call assume_init().
The following code illustrates the pattern detected by this rule:
use std::mem::MaybeUninit;
pub fn bad() {
// FLAGGED: MaybeUninit::uninit().assume_init() reads uninitialised memory
let x: u32 = unsafe { MaybeUninit::uninit().assume_init() };
println!("{}", x);
}