This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/63303] Pointer subtraction is broken when using -fsanitize=undefined
- From: "jakub at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Fri, 19 Sep 2014 15:57:46 +0000
- Subject: [Bug c/63303] Pointer subtraction is broken when using -fsanitize=undefined
- Auto-submitted: auto-generated
- References: <bug-63303-4 at http dot gcc dot gnu dot org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63303
--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to mikulas from comment #4)
> ... and another related problem (try this on 32-bit system):
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void)
> {
> short *a = malloc(0x50000000 * sizeof(short));
> short *b = a + 0x50000000;
> printf("%ld\n", (long)(b - a));
> return 0;
> }
>
> Here, the return value should be positive (0x50000000), but it is negative.
> IMHO, according to the C standard, this is program correct and positive
> result should be returned.
This testcase is invalid, you really can't have an object bigger than half of
the address space in C/C++, pointer difference is signed ptrdiff_t and if you
have larger object, you can't subtract arbitrary char pointers in it anymore.
If you need more than 2GB in a single array, just use 64-bit system.