Description
Slicing, indexing, and other safe operations on slices and strings pervasively use <*T>::offset
and APIs built on top of it. These have the requirement that
Both the starting and resulting pointer must be either in bounds or one byte past the end of the same allocated object.
So if one allocates two pieces of memory and after proper checking miraculously finds they are directly adjacent, one can't safely construct a slice/str/etc. that spans both of these allocations. At least, one can't do very many things with the result it without causing UB from crossing the boundary between the allocations.
I couldn't find anything documenting this. It should be noted on the unsafe constructors (from_raw_parts
etc.) at minimum. These already link to offset
's documentation but only refer to its "no larger than isize::MAX" requirement, with no mention that the other requirements are also relevant.
cc oberien/str-concat#8
cc @rust-lang/wg-unsafe-code-guidelines
(Similar issues apply to references-to-arrays and field accesses in aggregates, but this is due to the compiler's codegen for language primitives rather than due to standard library code, so it should go into the UCG and I believe we're more or less covering that already.)