Skip to content

Commit cc0d634

Browse files
committed
Liballoc IntoIter limit unsafe to pointer arithmethic
1 parent 2b7f87b commit cc0d634

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

library/alloc/src/vec.rs

+14-18
Original file line numberDiff line numberDiff line change
@@ -2697,25 +2697,21 @@ impl<T> Iterator for IntoIter<T> {
26972697

26982698
#[inline]
26992699
fn next(&mut self) -> Option<T> {
2700-
unsafe {
2701-
if self.ptr as *const _ == self.end {
2702-
None
2703-
} else {
2704-
if mem::size_of::<T>() == 0 {
2705-
// purposefully don't use 'ptr.offset' because for
2706-
// vectors with 0-size elements this would return the
2707-
// same pointer.
2708-
self.ptr = arith_offset(self.ptr as *const T, 1) as *mut T;
2709-
2710-
// Make up a value of this ZST.
2711-
Some(mem::zeroed())
2712-
} else {
2713-
let old = self.ptr;
2714-
self.ptr = self.ptr.offset(1);
2700+
if self.ptr as *const _ == self.end {
2701+
None
2702+
} else if mem::size_of::<T>() == 0 {
2703+
// purposefully don't use 'ptr.offset' because for
2704+
// vectors with 0-size elements this would return the
2705+
// same pointer.
2706+
self.ptr = unsafe { arith_offset(self.ptr as *const T, 1) as *mut T };
2707+
2708+
// Make up a value of this ZST.
2709+
Some(unsafe { mem::zeroed() })
2710+
} else {
2711+
let old = self.ptr;
2712+
self.ptr = unsafe { self.ptr.offset(1) };
27152713

2716-
Some(ptr::read(old))
2717-
}
2718-
}
2714+
Some(unsafe { ptr::read(old) })
27192715
}
27202716
}
27212717

0 commit comments

Comments
 (0)