public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/66773] New: sign-compare warning for == and != are pretty useless
@ 2015-07-06  8:46 daniel.marjamaki at gmail dot com
  2015-07-06 17:00 ` [Bug c/66773] " segher at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: daniel.marjamaki at gmail dot com @ 2015-07-06  8:46 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 66773
           Summary: sign-compare warning for == and != are pretty useless
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: daniel.marjamaki at gmail dot com
  Target Milestone: ---

I wrote a clang bug report:
https://llvm.org/bugs/show_bug.cgi?id=24036

I recommend that -Wsign-compare is not written for == and != comparisons.

For relational comparisons the sign makes a direct difference, the result of 'a
> b' can be different if you do a sign-cast of an operand.

For equality comparisons the sign does not make a direct difference. the result
of 'a == b' is the same even if you sign-cast an operand.

Code example:

void f(signed int a, unsigned int b) {
  if (a == b) {}
}

gcc writes this warning:

signcompare.c:3:19: warning: comparison between signed and unsigned integer
expressions [-Wsign-compare]

In my humble opinion the risk of a real bug here is really low. a has to be
negative. b has to be really large (unlikely). the bitpatterns of a and b has
to match. if the bitpatterns do match it might actually be the intention that
the test should succeed. but if that is not intentional then there is a bug.

The proper fix for this is to write:

  if (a >= 0 && a == b) {}

However I have seen that this is fixed wrongly by a useless cast. 

This kind of false positive is indirectly a security problem. People routinely
hide these false positives using casts or changed variable types etc. and that
cause bugs and hides other real warnings.

In my humble opinion the risk of a bug here is really low.

The proper fix for this is to write:

  if (a >= 0 && a == b) {}

However I have seen that this is fixed by a useless cast. 

This kind of false positive is indirectly a security problem. People routinely
hide these false positives using casts or changed variable types etc. and that
cause bugs and hides other real warnings.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-11-13 10:08 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-06  8:46 [Bug c/66773] New: sign-compare warning for == and != are pretty useless daniel.marjamaki at gmail dot com
2015-07-06 17:00 ` [Bug c/66773] " segher at gcc dot gnu.org
2015-07-06 18:23 ` daniel.marjamaki at gmail dot com
2015-07-06 23:06 ` daniel.marjamaki at gmail dot com
2015-07-07  6:50 ` schwab@linux-m68k.org
2021-11-13 10:08 ` egallager at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).