Skip to content

Commit dfc75da

Browse files
joboetgitbot
authored and
gitbot
committed
alloc: remove unsound IsZero for raw pointers
Fixes rust-lang#135338
1 parent 5f93459 commit dfc75da

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

Diff for: alloc/src/vec/is_zero.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,8 @@ impl_is_zero!(char, |x| x == '\0');
4040
impl_is_zero!(f32, |x: f32| x.to_bits() == 0);
4141
impl_is_zero!(f64, |x: f64| x.to_bits() == 0);
4242

43-
unsafe impl<T> IsZero for *const T {
44-
#[inline]
45-
fn is_zero(&self) -> bool {
46-
(*self).is_null()
47-
}
48-
}
49-
50-
unsafe impl<T> IsZero for *mut T {
51-
#[inline]
52-
fn is_zero(&self) -> bool {
53-
(*self).is_null()
54-
}
55-
}
43+
// `IsZero` cannot be soundly implemented for pointers because of provenance
44+
// (see #135338).
5645

5746
unsafe impl<T: IsZero, const N: usize> IsZero for [T; N] {
5847
#[inline]

Diff for: alloc/tests/vec.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2742,3 +2742,13 @@ fn max_swap_remove() {
27422742
let mut v = vec![0];
27432743
v.swap_remove(usize::MAX);
27442744
}
2745+
2746+
// Regression test for #135338
2747+
#[test]
2748+
fn vec_null_ptr_roundtrip() {
2749+
let ptr = std::ptr::from_ref(&42);
2750+
let zero = ptr.with_addr(0);
2751+
let roundtripped = vec![zero; 1].pop().unwrap();
2752+
let new = roundtripped.with_addr(ptr.addr());
2753+
unsafe { new.read() };
2754+
}

0 commit comments

Comments
 (0)