From 2b7f87b5fa43336ed1237747f60fd9095a41ea3d Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Thu, 2 Jul 2020 12:11:57 +0800 Subject: [PATCH 1/3] Liballoc tweak use *const T instead of *const i8 *const T is also used in the same parts and also used for arith_offset. --- library/alloc/src/vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/vec.rs b/library/alloc/src/vec.rs index 786d1b6ba82..2ff82a5dd3f 100644 --- a/library/alloc/src/vec.rs +++ b/library/alloc/src/vec.rs @@ -2705,7 +2705,7 @@ fn next(&mut self) -> Option { // purposefully don't use 'ptr.offset' because for // vectors with 0-size elements this would return the // same pointer. - self.ptr = arith_offset(self.ptr as *const i8, 1) as *mut T; + self.ptr = arith_offset(self.ptr as *const T, 1) as *mut T; // Make up a value of this ZST. Some(mem::zeroed()) From cc0d6345500932e8118ba65e98944a6a3bac3277 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Thu, 2 Jul 2020 12:13:17 +0800 Subject: [PATCH 2/3] Liballoc IntoIter limit unsafe to pointer arithmethic --- library/alloc/src/vec.rs | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/library/alloc/src/vec.rs b/library/alloc/src/vec.rs index 2ff82a5dd3f..aefcbf5ad5d 100644 --- a/library/alloc/src/vec.rs +++ b/library/alloc/src/vec.rs @@ -2697,25 +2697,21 @@ impl Iterator for IntoIter { #[inline] fn next(&mut self) -> Option { - unsafe { - if self.ptr as *const _ == self.end { - None - } else { - if mem::size_of::() == 0 { - // purposefully don't use 'ptr.offset' because for - // vectors with 0-size elements this would return the - // same pointer. - self.ptr = arith_offset(self.ptr as *const T, 1) as *mut T; + if self.ptr as *const _ == self.end { + None + } else if mem::size_of::() == 0 { + // purposefully don't use 'ptr.offset' because for + // vectors with 0-size elements this would return the + // same pointer. + self.ptr = unsafe { arith_offset(self.ptr as *const T, 1) as *mut T }; - // Make up a value of this ZST. - Some(mem::zeroed()) - } else { - let old = self.ptr; - self.ptr = self.ptr.offset(1); + // Make up a value of this ZST. + Some(unsafe { mem::zeroed() }) + } else { + let old = self.ptr; + self.ptr = unsafe { self.ptr.offset(1) }; - Some(ptr::read(old)) - } - } + Some(unsafe { ptr::read(old) }) } } From 50315238aa8ffae08f29b260aa36511e03b5e070 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Sun, 5 Jul 2020 12:27:23 +0800 Subject: [PATCH 3/3] Liballoc DoubleEndedIterator limit unsafe to pointer arithmethic --- library/alloc/src/vec.rs | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/library/alloc/src/vec.rs b/library/alloc/src/vec.rs index aefcbf5ad5d..559030e6c8a 100644 --- a/library/alloc/src/vec.rs +++ b/library/alloc/src/vec.rs @@ -2703,7 +2703,7 @@ fn next(&mut self) -> Option { // purposefully don't use 'ptr.offset' because for // vectors with 0-size elements this would return the // same pointer. - self.ptr = unsafe { arith_offset(self.ptr as *const T, 1) as *mut T }; + self.ptr = unsafe { arith_offset(self.ptr as *const i8, 1) as *mut T }; // Make up a value of this ZST. Some(unsafe { mem::zeroed() }) @@ -2735,22 +2735,18 @@ fn count(self) -> usize { impl DoubleEndedIterator for IntoIter { #[inline] fn next_back(&mut self) -> Option { - unsafe { - if self.end == self.ptr { - None - } else { - if mem::size_of::() == 0 { - // See above for why 'ptr.offset' isn't used - self.end = arith_offset(self.end as *const i8, -1) as *mut T; + if self.end == self.ptr { + None + } else if mem::size_of::() == 0 { + // See above for why 'ptr.offset' isn't used + self.end = unsafe { arith_offset(self.end as *const i8, -1) as *mut T }; - // Make up a value of this ZST. - Some(mem::zeroed()) - } else { - self.end = self.end.offset(-1); + // Make up a value of this ZST. + Some(unsafe { mem::zeroed() }) + } else { + self.end = unsafe { self.end.offset(-1) }; - Some(ptr::read(self.end)) - } - } + Some(unsafe { ptr::read(self.end) }) } } }