From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3816 invoked by alias); 19 Sep 2014 13:47:41 -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 3732 invoked by uid 48); 19 Sep 2014 13:47:37 -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 13:47: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: cc 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/msg01947.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63303 Jakub Jelinek changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jason at gcc dot gnu.org, | |jsm28 at gcc dot gnu.org, | |rguenth at gcc dot gnu.org --- Comment #3 from Jakub Jelinek --- 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.