This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: gcc-3.4.4-20050211: maybe a danger behaviour
- From: James E Wilson <wilson at specifixinc dot com>
- To: Denis Zaitsev <zzz at anda dot ru>
- Cc: gcc at gcc dot gnu dot org
- Date: Thu, 10 Mar 2005 13:49:32 -0800
- Subject: Re: gcc-3.4.4-20050211: maybe a danger behaviour
- References: <20050226234210.A2223@macacos.ward.six>
Denis Zaitsev wrote:
a) Formally, the code is correct. As p->what can never be < 0
according to its type.
I am assuming this is C code. C and C++ have different rules for enums.
This isn't strictly true. The C standard does not say that enums are
unsigned. It only says that they are equivalent to an integral type
with a few restrictions, and an implementation can choose to make it
signed or unsigned when all enum values are non-negative. So depending
on the compiler, it could be possible for p->what to be negative.
b) GCC _silently_ allows the {-1} initialization for that type, even
with -Wall.
This is because there is no such thing as an enum type in C, enums are
just an alias for an integral type, and it is never wrong to store -1 in
an integral type, even if it is unsigned. (C++ on the other hand will
complain here because C++ has actual enum types distinct from integral
types.)
Uncommenting the c= -1 member of enum, or explicit casting p->what to
int solves the problem, of course. But maybe some warning would be
appropriate in such a situation?
You will get a warning if you compile with -W.
tmp.c:22: warning: comparison of unsigned expression >= 0 is always true
--
Jim Wilson, GNU Tools Support, http://www.SpecifixInc.com