From cc0d6345500932e8118ba65e98944a6a3bac3277 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Thu, 2 Jul 2020 12:13:17 +0800 Subject: [PATCH] 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) }) } }