Undefined behaviour in slice::fill
specialization. #87891
Description
opened on Aug 9, 2021
This was reported by @the8472 on Zulip: https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/potential.20UB.20in.20slice.3A.3Afill/near/248871405
I'm wondering if the optimization here is correct:
rust/library/core/src/slice/specialize.rs
Lines 22 to 28 in ae90dcf
Specifically if T is
MaybeUninit<u8>
. It momentarily transmutes that to an u8 before passing it towrite_bytes
.
Demo:
use std::mem::MaybeUninit;
fn main() {
let mut a = [MaybeUninit::<u8>::uninit(); 10];
a.fill(MaybeUninit::uninit());
}
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
--> /home/mara/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/intrinsics.rs:2191:14
|
2191 | unsafe { write_bytes(dst, val, count) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Metadata
Assignees
Labels
Area: raw pointers, MaybeUninit, NonNullArea: `[T]`Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.Relevant to the library team, which will review and decide on the PR/issue.
Activity