This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: gcc doesn't warn that (unsigned > 0) is always true
On Mon, 20 Oct 2003 12:12:08 -0400
Daniel Jacobowitz <drow@mvista.com> wrote:
> On Mon, Oct 20, 2003 at 06:09:55PM +0200, David Calinski wrote:
> > Hi all,
> >
> > This may be a bit stupid, but as I run into problem once because of
> > this...
> > I think gcc should warn about it when "-Wall" is set.
> >
> > Consider the following snip of code:
> >
> >
> > /* gcc (3.3.2 with -Wall and -pedantic)
> > * doesn't warn that (unsigned > 0) is always true.
> > */
> >
> > #include <stdio.h>
> >
> > void a_loop (unsigned int in) {
> > int dumb = 0;
> > do {
> > ++dumb; /* anything here */
> >
> > /* Well, a lot of code can be here,
> > * almost all variables int _signed_,
> > * so a programmer may overlook that 'in' is_unsigned_
> > */
> > } while (--in > 0);
> >
> > /* variable 'in' will be NEVER less than zero (it's unsigned),
> > * this is in fact an *endless* loop.
> > * It should be obvious that it wasn't programmer intention.
> > * Shouldn't gcc with -Wall warn about it?
> > * Shouldn't gcc warn about all such obviously mistaken checks
> > * (unsigned > 0) that are always true?
> > */
> > }
>
> That is not an endless loop. in == 0 will terminate it.
Right, sorry, my mistake. But it originally was while(--in >= 0), I
just have experimented with it and posted the wrong code. *ashamed*
unsigned int in = 0;
do {
/* anything */
} while (--in >= 0);
is and endless loop and gcc doesn't warn about it.
--
David Calinski