mem::uninitialized or mem::zeroed used to produce a value
ID |
rust.correctness.mem_uninitialized_mem_zeroed_replace |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
high |
Remediation Effort |
medium |
Resource |
Undefined Behaviour |
Language |
Rust |
Description
mem::uninitialized and mem::zeroed fabricate a value without running any constructor, which is instant undefined behaviour for a type that has invalid bit patterns - a reference, a Box, an enum, a Vec. Use MaybeUninit, or build the value properly with Default::default().
Rationale
mem::uninitialized and mem::zeroed fabricate a value without running any constructor, which is instant undefined behaviour for a type that has invalid bit patterns - a reference, a Box, an enum, a Vec. Use MaybeUninit, or build the value properly with Default::default().
The following code illustrates the pattern detected by this rule:
use std::mem;
pub fn zeroed_vec() {
// FLAGGED: mem::uninitialized or mem::zeroed used to produce a value
let v: Vec<u8> = unsafe { mem::zeroed() };
println!("{:?}", v);
}