Description
The core::slice
code appears to have been written to work correctly for slices of zero-sized elements where s.as_ptr() + s.len()
overflows. Specifically, the iterators calculate an end
value like that, but doesn't rely on end > ptr
(it does equality tests, and for size_hint
it calculates the delta), and the iterators also explicitly always yield &mut *(1 as *mut _)
as the pointer value for zero-sized types to avoid the case of yielding null (which would be interpreted as None
instead of Some(_)
).
But all this code was written before we had overflow checks. With overflow checks enabled, there's a number of places that can now inadvertently overflow. As such, the whole module needs to be looked over and converted to use wrapping arithmetic where it makes sense.
Also see #24997.