https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95279

--- Comment #10 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to Marc Glisse from comment #8)
> (In reply to Jakub Jelinek from comment #4)
> > There is nothing wrong on addition of -1, whether signed or cast to
> > size_t/uintptr_t, to a pointer,
> 
> Looking at the standard (I am not a pro at that), one could easily interpret
> that p+(size_t)(-1) means adding a huge number to p, not subtracting 1. It
> does not say that the integer is cast to ptrdiff_t or anything like that.

I don't read it that way.
Let's use e.g. the C++ wording:
"Otherwise, if P points to an array element i of an array object x with n
elements, the expressions P + J and J + P (where J has the value j) point to
the (possibly-hypothetical) array element i+j of x if 0≤i+j≤n and the
expression P - J points to the (possibly-hypothetical) array element i−j of x
if 0≤i−j≤n.
In the (#c4 testcase with the s/p + 1/buf + 1/ fix), i is 1, and j is either
-1 or (size_t) -1.  For both, 1 + -1 or 1 + (size_t) -1 give 0 and thus the
pointer addition is well defined and points to the first element of the object.

Reply via email to