From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 30058 invoked by alias); 23 Sep 2013 19:43:00 -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 30048 invoked by uid 89); 23 Sep 2013 19:43:00 -0000 Received: from mailout05.t-online.de (HELO mailout05.t-online.de) (194.25.134.82) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 23 Sep 2013 19:43:00 +0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=2.5 required=5.0 tests=AWL,BAYES_50,RCVD_IN_BRBL_LASTEXT,RCVD_IN_PBL,RCVD_IN_SEMBLACK,UNPARSEABLE_RELAY autolearn=no version=3.3.2 X-HELO: mailout05.t-online.de Received: from fwd09.aul.t-online.de (fwd09.aul.t-online.de ) by mailout05.t-online.de with smtp id 1VOC20-0003M8-Ht; Mon, 23 Sep 2013 21:42:52 +0200 Received: from [192.168.0.103] (Jlv4drZ6ZhyU97jNnYI+QM676OsIJoHoOE3UaPZ9BqUM-Y8+BnJYaxMmXkuGmHvQkU@[84.175.192.135]) by fwd09.t-online.de with esmtp id 1VOC1t-4BwiSe0; Mon, 23 Sep 2013 21:42:45 +0200 Message-ID: <1379965364.3915.221.camel@yam-132-YW-E178-FTW> Subject: Re: how to make gcc warn about arithmetic signed overflow From: Oleg Endo To: Dave Allured - NOAA Affiliate Cc: gcc-help@gcc.gnu.org Date: Mon, 23 Sep 2013 19:43:00 -0000 In-Reply-To: References: <20130921164609.GC3086@a.lan> <20130921174229.GD3086@a.lan> <20130923000355.fa2a964c.jklowden@schemamania.org> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit Mime-Version: 1.0 X-IsSubscribed: yes X-SW-Source: 2013-09/txt/msg00160.txt.bz2 On Mon, 2013-09-23 at 13:38 -0600, Dave Allured - NOAA Affiliate wrote: > On Sun, Sep 22, 2013 at 10:03 PM, James K. Lowden > wrote: > > Regarding the OP's query > > > >> > int r = ab * bc; > > > > although the provided example is simple enough, it's the compiler's > > job is to generate object code, not to do static analysis. > > > > Even if the values are const, in the general case they could be > > modified by another module or another thread. The compiler simply > > doesn't have enough information to warn of every runtime overflow. > > 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. > See -ftrapv option. Although trapping math is not supported on every target properly. E.g. http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35412 or SH related http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54272 Cheers, Oleg