From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2051 invoked by alias); 23 Sep 2013 22:00:28 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 2038 invoked by uid 89); 23 Sep 2013 22:00:27 -0000 Received: from asbnvacz-mailrelay01.megapath.net (HELO asbnvacz-mailrelay01.megapath.net) (207.145.128.243) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 23 Sep 2013 22:00:27 +0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 X-HELO: asbnvacz-mailrelay01.megapath.net Received: from mail5.sea5.speakeasy.net (mail5.sea5.speakeasy.net [69.17.117.49]) by asbnvacz-mailrelay01.megapath.net (Postfix) with ESMTP id F1AB81EE5CAE for ; Mon, 23 Sep 2013 18:00:24 -0400 (EDT) Received: (qmail 1442 invoked from network); 23 Sep 2013 22:00:24 -0000 Received: by simscan 1.4.0 ppid: 18358, pid: 7014, t: 0.5606s scanners: clamav: 0.88.2/m:52/d:10739 Received: from unknown (HELO oak.schemamania.org) ([216.254.83.208]) (envelope-sender ) by mail5.sea5.speakeasy.net (qmail-ldap-1.03) with SMTP for ; 23 Sep 2013 22:00:24 -0000 Received: from oak.schemamania.org (localhost [IPv6:::1]) by oak.schemamania.org (Postfix) with SMTP id 8D61A30E0F7E for ; Mon, 23 Sep 2013 18:00:22 -0400 (EDT) Date: Mon, 23 Sep 2013 22:00:00 -0000 From: "James K. Lowden" To: gcc-help@gcc.gnu.org Subject: Re: how to make gcc warn about arithmetic signed overflow Message-Id: <20130923180022.b06c9ae2.jklowden@schemamania.org> In-Reply-To: <52409B07.1070002@redhat.com> References: <20130921164609.GC3086@a.lan> <20130921174229.GD3086@a.lan> <20130923000355.fa2a964c.jklowden@schemamania.org> <52409B07.1070002@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2013-09/txt/msg00164.txt.bz2 On Mon, 23 Sep 2013 20:48:23 +0100 Andrew Haley wrote: > On 09/23/2013 08:38 PM, Dave Allured - NOAA Affiliate wrote: > > I believe the CPU overflow flag is updated after most integer > > arithmetic instructions. Does GCC have any facility for checking > > this flag after each integer operation? This would be a runtime > > check, of course, not a compile time check. > > It wouldn't help with optimized code. GCC reorganizes code, and it > assumes that overflow doesn't happen. GCC inserts some arithmetic > instructions while optimizing and deletes others. So, even if an > overflow happens in your code, it doesn't necessarily happen at > runtime. Could you unpack that a bit? Regardless of optimization, the CPU, not the compiler, executes the ADD or MUL operation, or whatever, and sets or does not set the overflow bit accordingly, right? Why can't the compiler generate code that senses that, and raises a runtime error? It's no different than answering divide-by-zero. I've written a lot of SAFE_CAST macros that check the return of sizeof or strlen(3) before casting it to an int and assigning the result to something that *must* be an int. That code is terribly inefficient, clumsy to read, noise on the screen, really. But made necessary IMO because the compiler conceals what the processor reports. --jkl