From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 31141 invoked by alias); 21 Sep 2013 16:45:35 -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 31131 invoked by uid 89); 21 Sep 2013 16:45:35 -0000 Received: from mail-ee0-f46.google.com (HELO mail-ee0-f46.google.com) (74.125.83.46) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Sat, 21 Sep 2013 16:45:35 +0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00,FREEMAIL_FROM autolearn=ham version=3.3.2 X-HELO: mail-ee0-f46.google.com Received: by mail-ee0-f46.google.com with SMTP id c13so854526eek.19 for ; Sat, 21 Sep 2013 09:45:30 -0700 (PDT) X-Received: by 10.14.5.3 with SMTP id 3mr4011139eek.49.1379781930804; Sat, 21 Sep 2013 09:45:30 -0700 (PDT) Received: from localhost (89-70-245-0.dynamic.chello.pl. [89.70.245.0]) by mx.google.com with ESMTPSA id f49sm28114531eec.7.1969.12.31.16.00.00 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Sat, 21 Sep 2013 09:45:30 -0700 (PDT) Date: Sat, 21 Sep 2013 16:45:00 -0000 From: wempwer@gmail.com To: gcc-help@gcc.gnu.org Subject: how to make gcc warn about arithmetic signed overflow Message-ID: <20130921164609.GC3086@a.lan> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-IsSubscribed: yes X-SW-Source: 2013-09/txt/msg00132.txt.bz2 Hello, I am trying to C learn language on a quite high level. I spend a couple of days learning about unsigned and signed arithmetic/conversion overflow, integer promotion and arithmetic conversion. From what I understand the following snippet causes an undefined behavior on all platforms: int ab = 50000; int bc = 50000; int r = ab * bc; In the first and second line we assign 50000 to signed int, nothing bad happens here because on my computers int is 32 bits long so there is no overflow. If it was an overflow, it is implementation defined and would cause wraparound on most platforms. However, in the third line there is no integer promotion performed because both operands are already of type int but we have an arithmetic overflow because 50000 * 50000 doesn't fit in 32 bits signed integer. According to the C standard this is an undefined behavior but again on most platforms it comes down to wraparound. Value r is printed in printf with %d specifier as -1794967296 using two's complement mechanism. To my surprise gcc doesn't print any warnings in the 3rd line. I tried several options such as -Wall, -Wstrict-overflow=5, -pedantic, -Wextra but nothing produces a warning. Is it possible for gcc to produce a warning in such situation? --