https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66773

--- Comment #20 from Daniel Marjamäki <daniel.marjamaki at gmail dot com> ---
(In reply to Segher Boessenkool from comment #15)
> (In reply to Daniel Marjamäki from comment #12)
> > So, how would you fix the warning for `f`? Many programmers would "fix" it
> > with a cast.
> > 
> > Assuming that `s` and `u` can have arbitrary values, here is the proper 
> > code:
> > 
> > void f(long s, unsigned long u) { if (s >= 0 && s == u) g(); }
> > 
> > For this correct code, gcc warns.
> 
> A much better fix is
> 
> void f1(long s, unsigned long u) { unsigned long su = s; if (su == u) g(); }
> 
> which makes it rather explicit what is going on.
> 
> Still much better is to not mixed signedness in types at all.

Ping. Your "much better" code does not work. This code prints "equal" on the
screen:


void f1(long s, unsigned long u) {
    unsigned long su = s;
    if (su == u) printf("equal\n");
}

int main() { f1(-1L, ~0UL); return 0; }


Please try again.

You proved my point somewhat. The programmer gets a warning, the programmer
tries to fix it, the code still has the same bug but the warning has gone away.
However I feel that your fix is much safer than a cast because Cppcheck,
sanitizers, etc can still warn.

Reply via email to