From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 13280 invoked by alias); 8 Aug 2002 08:56:01 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 13258 invoked by uid 71); 8 Aug 2002 08:56:01 -0000 Resent-Date: 8 Aug 2002 08:56:01 -0000 Resent-Message-ID: <20020808085601.13257.qmail@sources.redhat.com> Resent-From: gcc-gnats@gcc.gnu.org (GNATS Filer) Resent-To: nobody@gcc.gnu.org Resent-Cc: gcc-prs@gcc.gnu.org, gcc-bugs@gcc.gnu.org Resent-Reply-To: gcc-gnats@gcc.gnu.org, jss@ast.cam.ac.uk Received: (qmail 9319 invoked by uid 61); 8 Aug 2002 08:54:04 -0000 Message-Id: <20020808085404.9318.qmail@sources.redhat.com> Date: Thu, 08 Aug 2002 02:26:00 -0000 From: jss@ast.cam.ac.uk Reply-To: jss@ast.cam.ac.uk To: gcc-gnats@gcc.gnu.org X-Send-Pr-Version: gnatsweb-2.9.3 (1.1.1.1.2.31) Subject: c++/7536: C++ front end does not warn of promotion of negative ints to unsigneds X-SW-Source: 2002-08/txt/msg00156.txt.bz2 List-Id: >Number: 7536 >Category: c++ >Synopsis: C++ front end does not warn of promotion of negative ints to unsigneds >Confidential: no >Severity: non-critical >Priority: low >Responsible: unassigned >State: open >Class: sw-bug >Submitter-Id: net >Arrival-Date: Thu Aug 08 01:56:01 PDT 2002 >Closed-Date: >Last-Modified: >Originator: jss@ast.cam.ac.uk >Release: gcc-3.1 >Organization: >Environment: gcc-3.1 (RedHat RPM 3.1-1), on RedHat Linux 7.3 on an AMD Athlon (compiling for i386) >Description: An expression int+unsigned or int-unsigned promotes the int variable to an unsigned before doing the operation. Quite often this can do the /wrong/ thing, just like comparisons of ints and unsigneds. g++ should warn for obvious mistakes (as the c front end does), and should optionally warn for less obvious mistakes. e.g. unsigned a = 15; double b = -30 - a; should issue a warning, as b ends up at around 2^32, and probably not the expected -15. Further possibilities for warning are: unsigned a = 15; int b = -30; double c = b-c (the same thing). Another possibility is for warning for every implicit int/unsigned arithmetic operation/promotion, as the int could be negative. For instance: double dostuff(int i) { const unsigned cx = 10; return i-cx; } int main() { dostuff(-10); // dangerous return 0; } This can hide a serious error. However many programs will give lots of warnings. Maybe this could go into -Wconversion? The operation int+unsigned usually works (due to twos-complement representation - is this part of the standard?), however things like int a = -10; unsigned b = 20; double c = b + a; should also be flagged if the operation relies on non-defined behaviour. I'll include a test-case for warnings. >How-To-Repeat: Compile above with g++ -W -Wall -Wconversions -O2 test.cc >Fix: >Release-Note: >Audit-Trail: >Unformatted: ----gnatsweb-attachment---- Content-Type: application/octet-stream; name="test.cc" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="test.cc" I2luY2x1ZGUgPGlvc3RyZWFtPgoKLy8gaW50IHByb21vdGlvbiB0ZXN0LWNhc2UgKEplcmVteSBT YW5kZXJzIDIwMDIpCgp2b2lkIHRlc3QxKCkKewogIHVuc2lnbmVkIHUgPSAyMDsKICBkb3VibGUg ZCA9IC0xMCAtIHU7ICAvLyBzaG91bGQgd2FybiBmb3IgdGhpcyAKICBzdGQ6OmNvdXQgPDwgZCA8 PCAnXG4nOwp9Cgp2b2lkIHRlc3QyKCkKewogIHVuc2lnbmVkIHUgPSAyMDsKICBkb3VibGUgZCA9 IC0xMCArIHU7ICAvLyB3b3JrcyBidXQgc2hvdWxkIGJlIHdhcm5lZCBmb3IKICBzdGQ6OmNvdXQg PDwgZCA8PCAnXG4nOwp9Cgp2b2lkIHRlc3QzKCkKewogIHVuc2lnbmVkIHUgPSAyMDsKICBpbnQg aSA9IC0xMDsKICBkb3VibGUgZCA9IGkgLSB1OyAgLy8gc2hvdWxkIHdhcm4gZm9yIHRoaXMKICBz dGQ6OmNvdXQgPDwgZCA8PCAnXG4nOwp9Cgp2b2lkIHRlc3Q0KCkKewogIHVuc2lnbmVkIHUgPSAy MDsKICBpbn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