Documentation of wrapping_add/-_sub/-_offset need clarification... #80306
Labels
A-docs
Area: documentation for any part of the project, including the compiler, standard library, and tools
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
T-lang
Relevant to the language team, which will review and decide on the PR/issue.
T-libs-api
Relevant to the library API team, which will review and decide on the PR/issue.
...about whether pointer arithmetic is allowed to - temporarily - leave the bounds of an object, then go back into bounds and afterwards dereference the pointer.
The question is if code like this:
is sound or unsound.
The current documentation says:
So it specifies that the resulting pointer “remains attached to the same allocated object that
self
points to”, which suggests that after going back into bounds, dereferencing should be safe again.It also says “this method basically delays the requirement of staying within the same allocated object; ” which sounds like, staying within the same allocated object is still a requirement, which could be interpreted as that whenever any intermediate value did not stay within bounds, that intermediate value has some delayed violation of requirements attached to it that triggers UB once the final (back in bounds) pointer is dereferenced.
The following comparison, “
add
/sub
/offset
is immediate Undefined Behavior when crossing object boundaries;wrapping_add
/wrapping_sub
/wrapping_offset
produces a pointer but still leads to Undefined Behavior if that pointer is dereferenced” can easily be interpreted as confirming this interpretation, since object boundaries are crossed (twice, first going out of the object, then going back in) and the pointer is dereference afterwards (right after the second crossing of object boundaries).After I was addressing this on IRLO, @RalfJung gave the answer
@rustbot modify labels: T-doc, T-libs, T-lang, C-enhancement
Feel free to remove a
T-
label if this doesn’t seem relevant for that team.The text was updated successfully, but these errors were encountered: