On 29.10.2019 21:09, Jeff King wrote:
I think the issue is not the comparison, but rather that forming the
pointer "msg + off" is undefined, since its point outside of any object
(including the "one past" rule). And this is illegal in both C and C++,
Yes, thanks for clarifying.
> + p = msg + off < pend ? msg + off : pend - 1;
> though of course it works fine in practice most of the time.
The easiest thing that an optimizer can do (and that's actually quite
likely in my understanding) is to conclude that 'msg + off' is the only
non-UB branch here, therefore 'msg + off < pend' can be taken as always
true, and then discard 'pend - 1' branch. Afterall, UB serve the biggest
role in optimizations.
That's even more true now that all parts of equation are local and easy
to grasp for static analysis.