From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26295 invoked by alias); 6 Oct 2003 16:00:08 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 26277 invoked from network); 6 Oct 2003 16:00:08 -0000 Received: from unknown (HELO piper.synopsys.com) (198.182.56.5) by sources.redhat.com with SMTP; 6 Oct 2003 16:00:08 -0000 Received: (from jbuck@localhost) by piper.synopsys.com (8.11.6/8.11.6) id h96FxwO12926; Mon, 6 Oct 2003 08:59:58 -0700 Date: Mon, 06 Oct 2003 16:00:00 -0000 From: Joe Buck To: Falk Hueffner Cc: gcc@gcc.gnu.org Subject: Re: Suggested warning: "negating an expression of unsigned type does not yield a negative value" Message-ID: <20031006085958.A12894@synopsys.com> References: <87zngev8i4.fsf@student.uni-tuebingen.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5.1i In-Reply-To: <87zngev8i4.fsf@student.uni-tuebingen.de>; from falk.hueffner@student.uni-tuebingen.de on Mon, Oct 06, 2003 at 02:46:43PM +0200 X-SW-Source: 2003-10/txt/msg00156.txt.bz2 On Mon, Oct 06, 2003 at 02:46:43PM +0200, Falk Hueffner wrote: > I just found yet another bug of the kind: > > int f (int *p, unsigned x) { return p[-x]; } > > which only manifests on 64 bit platforms, because most (all?) > platforms have wrapping address arithmetic. The C and C++ standards require that unsigned values obey modulo 2**N arithmetic, so the value of -x is rigorously defined. > So I was wondering about a general warning about negating unsigned > values, since I couldn't really think of a legitimate application. There are legitimate applications, and I've used them in my code. > quick check with the gcc source turned up: > > gengtype-lex.l: > char *namestart; > size_t namelen; > [...] > for (namelen = 1; !ISSPACE (namestart[-namelen]); namelen++) > > This looks actually invalid to me, although it will probably work > everywhere. It's valid everywhere. > In fold_const.c, there's > > case RSHIFT_EXPR: > int2l = -int2l; > > also "invalid but works" since it's later passed to a function taking > int. Again, this is valid everywhere. > Then there's everybody's favourite idiom "x &= -x", but it can be > expressed clearer as "x &= ~x + 1". Again, it's fine as is. Just the fact that your proposed warning will turn on at least four complaints against correct usage in gcc shows that it is not a good idea.