[Bug c/63303] Pointer subtraction is broken when using -fsanitize=undefined
jakub at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Fri Sep 19 16:22:00 GMT 2014
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63303
--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to mikulas from comment #6)
> Regarding pointer difference, the C standard says this:
>
> When two pointers are subtracted, both shall point to elements of the same
> array object, or one past the last element of the array object; the result
> is the difference of the subscripts of the two array elements. The size of
> the result is implementation-defined, and its type (a signed integer type)
> is ptrdiff_t defined in the <stddef.h> header. If the result is not
> representable in an object of that type, the behavior is undefined. In other
> words, if the expressions P and Q point to, respectively, the i-th and j-th
> elements of an array object, the expression (P)-(Q) has the value i−j
> provided the value fits in an object of type ptrdiff_t.
>
> So: p points to the beginning, q points one past the last element, so the
> first condition is valid.
>
> The result is the difference of the subscripts of those two array elements:
> 0x50000000 - 0 = 0x50000000 - this is clearly representable in the type
> ptrdiff_t, so 0x50000000 result should be returned.
See what I wrote, any object size bigger than half of address space really
isn't supportable, because then (char *) (P) - (char *) (Q) might not fit into
ptrdiff_t. There is no point slowing down all pointer subtractions (other than
char/signed char/unsigned char pointers) for something that really wouldn't
work reliably anyway.
More information about the Gcc-bugs
mailing list