public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Suggested warning: "negating an expression of unsigned type does not yield a negative value"
@ 2003-10-06 12:46 Falk Hueffner
  2003-10-06 16:00 ` Joe Buck
  0 siblings, 1 reply; 6+ messages in thread
From: Falk Hueffner @ 2003-10-06 12:46 UTC (permalink / raw)
  To: gcc

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2003-10-06 17:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-06 12:46 Suggested warning: "negating an expression of unsigned type does not yield a negative value" Falk Hueffner
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

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).