diff options
| author | Ivan Tham <pickfire@riseup.net> | 2020-07-05 12:27:23 +0800 |
|---|---|---|
| committer | Ivan Tham <pickfire@riseup.net> | 2020-08-10 18:13:56 +0800 |
| commit | 50315238aa8ffae08f29b260aa36511e03b5e070 (patch) | |
| tree | 9054335e0fbf0901c6135344e651001178c08151 | |
| parent | cc0d6345500932e8118ba65e98944a6a3bac3277 (diff) | |
| download | rust-50315238aa8ffae08f29b260aa36511e03b5e070.tar.gz rust-50315238aa8ffae08f29b260aa36511e03b5e070.zip | |
Liballoc DoubleEndedIterator limit unsafe to pointer arithmethic
| -rw-r--r-- | library/alloc/src/vec.rs | 26 |
1 files 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 @@ impl<T> Iterator for IntoIter<T> { // 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 @@ impl<T> Iterator for IntoIter<T> { impl<T> DoubleEndedIterator for IntoIter<T> { #[inline] fn next_back(&mut self) -> Option<T> { - unsafe { - if self.end == self.ptr { - None - } else { - if mem::size_of::<T>() == 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::<T>() == 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) }) } } } |
