From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 11349 invoked by alias); 19 Sep 2014 15:57:52 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 11266 invoked by uid 48); 19 Sep 2014 15:57:47 -0000 From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/63303] Pointer subtraction is broken when using -fsanitize=undefined Date: Fri, 19 Sep 2014 15:57:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 4.9.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: jakub at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2014-09/txt/msg01963.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63303 --- Comment #5 from Jakub Jelinek --- (In reply to mikulas from comment #4) > ... and another related problem (try this on 32-bit system): > > #include > #include > > 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.