From 61f0a2b3fd961c9ae6d327d384bcffabf89a1c26 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 29 Aug 2018 23:08:47 +0200 Subject: [PATCH 1/2] fix some uses of pointer intrinsics with invalid pointers --- src/liballoc/vec.rs | 10 ++++------ src/libstd/collections/hash/table.rs | 4 +++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index e9c1a3df51891..02393a185b1da 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -2410,9 +2410,8 @@ impl Iterator for IntoIter { // same pointer. self.ptr = arith_offset(self.ptr as *const i8, 1) as *mut T; - // Use a non-null pointer value - // (self.ptr might be null because of wrapping) - Some(ptr::read(1 as *mut T)) + // Read from a properly aligned pointer to make up a value of this ZST. + Some(ptr::read(NonNull::dangling().as_ptr())) } else { let old = self.ptr; self.ptr = self.ptr.offset(1); @@ -2451,9 +2450,8 @@ impl DoubleEndedIterator for IntoIter { // See above for why 'ptr.offset' isn't used self.end = arith_offset(self.end as *const i8, -1) as *mut T; - // Use a non-null pointer value - // (self.end might be null because of wrapping) - Some(ptr::read(1 as *mut T)) + // Read from a properly aligned pointer to make up a value of this ZST. + Some(ptr::read(NonNull::dangling().as_ptr())) } else { self.end = self.end.offset(-1); diff --git a/src/libstd/collections/hash/table.rs b/src/libstd/collections/hash/table.rs index 768357ec8dc41..547f97cc8acee 100644 --- a/src/libstd/collections/hash/table.rs +++ b/src/libstd/collections/hash/table.rs @@ -742,7 +742,9 @@ impl RawTable { ) -> Result, CollectionAllocErr> { unsafe { let ret = RawTable::new_uninitialized_internal(capacity, fallibility)?; - ptr::write_bytes(ret.hashes.ptr(), 0, capacity); + if capacity > 0 { + ptr::write_bytes(ret.hashes.ptr(), 0, capacity); + } Ok(ret) } } From 357c5dacee1015dc03583287d0c7a132d9fe7880 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 16 Sep 2018 14:26:27 +0200 Subject: [PATCH 2/2] use mem::zeroed to make up ZST values --- src/liballoc/vec.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 02393a185b1da..7fc4453fec5b0 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -2410,8 +2410,8 @@ impl Iterator for IntoIter { // same pointer. self.ptr = arith_offset(self.ptr as *const i8, 1) as *mut T; - // Read from a properly aligned pointer to make up a value of this ZST. - Some(ptr::read(NonNull::dangling().as_ptr())) + // Make up a value of this ZST. + Some(mem::zeroed()) } else { let old = self.ptr; self.ptr = self.ptr.offset(1); @@ -2450,8 +2450,8 @@ impl DoubleEndedIterator for IntoIter { // See above for why 'ptr.offset' isn't used self.end = arith_offset(self.end as *const i8, -1) as *mut T; - // Read from a properly aligned pointer to make up a value of this ZST. - Some(ptr::read(NonNull::dangling().as_ptr())) + // Make up a value of this ZST. + Some(mem::zeroed()) } else { self.end = self.end.offset(-1);