Skip to content

Commit

Permalink
Liballoc IntoIter limit unsafe to pointer arithmethic
Browse files Browse the repository at this point in the history
  • Loading branch information
pickfire committed Aug 10, 2020
1 parent 2b7f87b commit cc0d634
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions library/alloc/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2697,25 +2697,21 @@ impl<T> Iterator for IntoIter<T> {

#[inline]
fn next(&mut self) -> Option<T> {
unsafe {
if self.ptr as *const _ == self.end {
None
} else {
if mem::size_of::<T>() == 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;

// Make up a value of this ZST.
Some(mem::zeroed())
} else {
let old = self.ptr;
self.ptr = self.ptr.offset(1);
if self.ptr as *const _ == self.end {
None
} else if mem::size_of::<T>() == 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(unsafe { mem::zeroed() })
} else {
let old = self.ptr;
self.ptr = unsafe { self.ptr.offset(1) };

Some(ptr::read(old))
}
}
Some(unsafe { ptr::read(old) })
}
}

Expand Down

0 comments on commit cc0d634

Please sign in to comment.