From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 81753 invoked by alias); 19 Oct 2015 11:03:34 -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 81625 invoked by uid 48); 19 Oct 2015 11:03:30 -0000 From: "rguenth at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/63303] Pointer subtraction is broken when using -fsanitize=undefined Date: Mon, 19 Oct 2015 11:03: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: rguenth at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: 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: 2015-10/txt/msg01485.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63303 --- Comment #12 from Richard Biener --- (In reply to Jakub Jelinek from comment #3) > The problem is that we don't have a POINTER_DIFF_EXPR similar to > POINTER_PLUS_EXPR, which would take two pointers and return an integer, and > the FEs emit pointer difference as cast of both the pointers to signed > integral type > and subtracts the integers. > If > ssize_t foo (char *p, char *q) { return p - q; } > is changed into > ssize_t foo (char *p, char *q) { return (ssize_t) p - (ssize_t) q; } > by the FE, then indeed if you have array starting at 0x7fff0000 and ending > at 0x80010000 and subtract those two pointers, you get undefined behavior. > That is undefined behavior not just for ubsan, but for anything else in the > middle-end. > So, if pointer difference is supposed to behave differently, then > we'd either need to represent pointer difference as > ssize_t foo (char *p, char *q) { return (ssize_t) ((size_t) p - (size_t) q); > } > (but we risk missed optimizations that way I'd say), or we'd need a better > representation of it in the middle-end. Note that apart from missing POINTER_DIFF this isn't a middle-end but a frontend issue.