This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Suggested warning: "negating an expression of unsigned type does not yield a negative value"


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:

  char *namestart;
  size_t namelen;
  for (namelen = 1; !ISSPACE (namestart[-namelen]); namelen++)

This looks actually invalid to me, although it will probably work

In fold_const.c, there's

      int2l = -int2l;

also "invalid but works" since it's later passed to a function taking

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?


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]