Skip to content

Commit

Permalink
Auto merge of #135339 - joboet:ptr-is-zero, r=Noratrieb
Browse files Browse the repository at this point in the history
alloc: remove unsound `IsZero` for raw pointers

Fixes #135338
  • Loading branch information
bors committed Jan 10, 2025
2 parents b1a7dfb + 4426e9a commit 760b6f8
Showing 2 changed files with 12 additions and 13 deletions.
15 changes: 2 additions & 13 deletions library/alloc/src/vec/is_zero.rs
Original file line number Diff line number Diff line change
@@ -40,19 +40,8 @@ impl_is_zero!(char, |x| x == '\0');
impl_is_zero!(f32, |x: f32| x.to_bits() == 0);
impl_is_zero!(f64, |x: f64| x.to_bits() == 0);

unsafe impl<T> IsZero for *const T {
#[inline]
fn is_zero(&self) -> bool {
(*self).is_null()
}
}

unsafe impl<T> IsZero for *mut T {
#[inline]
fn is_zero(&self) -> bool {
(*self).is_null()
}
}
// `IsZero` cannot be soundly implemented for pointers because of provenance
// (see #135338).

unsafe impl<T: IsZero, const N: usize> IsZero for [T; N] {
#[inline]
10 changes: 10 additions & 0 deletions library/alloc/tests/vec.rs
Original file line number Diff line number Diff line change
@@ -2742,3 +2742,13 @@ fn max_swap_remove() {
let mut v = vec![0];
v.swap_remove(usize::MAX);
}

// Regression test for #135338
#[test]
fn vec_null_ptr_roundtrip() {
let ptr = std::ptr::from_ref(&42);
let zero = ptr.with_addr(0);
let roundtripped = vec![zero; 1].pop().unwrap();
let new = roundtripped.with_addr(ptr.addr());
unsafe { new.read() };
}

0 comments on commit 760b6f8

Please sign in to comment.