From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20387 invoked by alias); 20 Dec 2014 15:24:38 -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 20199 invoked by uid 48); 20 Dec 2014 15:24:32 -0000 From: "mikpelinux at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug rtl-optimization/64294] invalid code, zero check gets optimized away Date: Sat, 20 Dec 2014 15:24:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: rtl-optimization X-Bugzilla-Version: 4.9.2 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: mikpelinux at gmail dot com 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-12/txt/msg02459.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64294 --- Comment #10 from Mikael Pettersson --- You're invoking undefined behaviour due to overflow in signed integer arithmetic. Running it after compiling with -fsanitize=undefined produces: petite.c:391:28: runtime error: signed integer overflow: 2147483647 * 2 cannot be represented in type 'int' Fixing that in the following crude way: --- petite.c 2014-12-20 16:02:59.786063515 +0100 +++ petite-fixed.c 2014-12-20 16:15:05.030889115 +0100 @@ -388,7 +388,7 @@ free(usects); return 1; } - backbytes = backbytes*2 + oob; + backbytes = (int)((unsigned int)backbytes*2 + (unsigned int)oob); if ( (oob = doubledl(&ssrc, &mydl, buf, bufsz)) == -1 ) { free(usects); return 1; allows the testcase to work at -O2 and -O3.