Skip to content

impl Random on arrays can lead to dropping uninitialized memory #5

@JOE1994

Description

@JOE1994

Hello 🦀 ,
we (Rust group @sslab-gatech) found a memory-safety/soundness issue in this crate while scanning Rust code on crates.io for potential vulnerabilities.

Issue Description

autorand-rs/src/lib.rs

Lines 160 to 170 in c838309

impl<T: Random> Random for [T; $s] {
fn random() -> Self {
unsafe {
let mut array: [T; $s] = std::mem::uninitialized();
for i in 0..$s {
std::ptr::write(&mut array[i], T::random());
}
array
}
}
}

T::random() can potentially panic (since Random is a public trait and users can implement it on custom types) and if it does,
the partially initialized array is dropped. Thus T::drop() can be invoked on uninitialized memory, leading to undefined behavior.

Thank you for reviewing this issue 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions