This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Suggested warning: "negating an expression of unsigned type does not yield a negative value"
- From: Falk Hueffner <falk dot hueffner at student dot uni-tuebingen dot de>
- To: gcc at gcc dot gnu dot org
- Date: 06 Oct 2003 14:46:43 +0200
- Subject: Suggested warning: "negating an expression of unsigned type does not yield a negative value"
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