From: Joe Buck <jbuck@synopsys.com>
To: Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
Cc: gcc@gcc.gnu.org
Subject: Re: Suggested warning: "negating an expression of unsigned type does not yield a negative value"
Date: Mon, 06 Oct 2003 16:00:00 -0000 [thread overview]
Message-ID: <20031006085958.A12894@synopsys.com> (raw)
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
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.
next prev parent reply other threads:[~2003-10-06 16:00 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-10-06 12:46 Falk Hueffner
2003-10-06 16:00 ` Joe Buck [this message]
2003-10-06 16:11 ` Falk Hueffner
2003-10-06 17:23 ` Jamie Lokier
2003-10-06 17:48 ` Joe Buck
2003-10-06 17:52 ` Falk Hueffner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20031006085958.A12894@synopsys.com \
--to=jbuck@synopsys.com \
--cc=falk.hueffner@student.uni-tuebingen.de \
--cc=gcc@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).