public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
To: gcc@gcc.gnu.org
Subject: Suggested warning: "negating an expression of unsigned type does not yield a negative value"
Date: Mon, 06 Oct 2003 12:46:00 -0000	[thread overview]
Message-ID: <87zngev8i4.fsf@student.uni-tuebingen.de> (raw)

Hi,

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.

So I was wondering about a general warning about negating unsigned
values, since I couldn't really think of a legitimate application. A
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.

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.

Then there's everybody's favourite idiom "x &= -x", but it can be
expressed clearer as "x &= ~x + 1".

Then there's constant folding in neg_double. Hm. Damn. I can't think
of any reformulation which does not obscure the code. So this warning
should probably not be turned on by -W. But it seems generally useful.

Any opinions?

-- 
	Falk

             reply	other threads:[~2003-10-06 12:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-06 12:46 Falk Hueffner [this message]
2003-10-06 16:00 ` Joe Buck
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=87zngev8i4.fsf@student.uni-tuebingen.de \
    --to=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).