Skip to content

Commit 61f0a2b

Browse files
committed
fix some uses of pointer intrinsics with invalid pointers
1 parent 29e6aab commit 61f0a2b

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/liballoc/vec.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -2410,9 +2410,8 @@ impl<T> Iterator for IntoIter<T> {
24102410
// same pointer.
24112411
self.ptr = arith_offset(self.ptr as *const i8, 1) as *mut T;
24122412

2413-
// Use a non-null pointer value
2414-
// (self.ptr might be null because of wrapping)
2415-
Some(ptr::read(1 as *mut T))
2413+
// Read from a properly aligned pointer to make up a value of this ZST.
2414+
Some(ptr::read(NonNull::dangling().as_ptr()))
24162415
} else {
24172416
let old = self.ptr;
24182417
self.ptr = self.ptr.offset(1);
@@ -2451,9 +2450,8 @@ impl<T> DoubleEndedIterator for IntoIter<T> {
24512450
// See above for why 'ptr.offset' isn't used
24522451
self.end = arith_offset(self.end as *const i8, -1) as *mut T;
24532452

2454-
// Use a non-null pointer value
2455-
// (self.end might be null because of wrapping)
2456-
Some(ptr::read(1 as *mut T))
2453+
// Read from a properly aligned pointer to make up a value of this ZST.
2454+
Some(ptr::read(NonNull::dangling().as_ptr()))
24572455
} else {
24582456
self.end = self.end.offset(-1);
24592457

src/libstd/collections/hash/table.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,9 @@ impl<K, V> RawTable<K, V> {
742742
) -> Result<RawTable<K, V>, CollectionAllocErr> {
743743
unsafe {
744744
let ret = RawTable::new_uninitialized_internal(capacity, fallibility)?;
745-
ptr::write_bytes(ret.hashes.ptr(), 0, capacity);
745+
if capacity > 0 {
746+
ptr::write_bytes(ret.hashes.ptr(), 0, capacity);
747+
}
746748
Ok(ret)
747749
}
748750
}

0 commit comments

Comments
 (0)