Skip to content

Commit

Permalink
Auto merge of #87892 - rust-lang:spec-fill-size-one-bye, r=the8472
Browse files Browse the repository at this point in the history
Remove size_of == 1 case from `fill` specialization.

Fixes #87891

See [discussion on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/potential.20UB.20in.20slice.3A.3Afill/near/248875743).
  • Loading branch information
bors committed Aug 11, 2021
2 parents d488de8 + 3838301 commit 362e0f5
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions library/core/src/slice/specialize.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use crate::mem::{size_of, transmute_copy};
use crate::ptr::write_bytes;

pub(super) trait SpecFill<T> {
fn spec_fill(&mut self, value: T);
}
Expand All @@ -19,17 +16,8 @@ impl<T: Clone> SpecFill<T> for [T] {

impl<T: Copy> SpecFill<T> for [T] {
fn spec_fill(&mut self, value: T) {
if size_of::<T>() == 1 {
// SAFETY: The size_of check above ensures that values are 1 byte wide, as required
// for the transmute and write_bytes
unsafe {
let value: u8 = transmute_copy(&value);
write_bytes(self.as_mut_ptr(), value, self.len());
}
} else {
for item in self.iter_mut() {
*item = value;
}
for item in self.iter_mut() {
*item = value;
}
}
}

0 comments on commit 362e0f5

Please sign in to comment.