[LLVMbugs] [Bug 24036] New: sign-compare warning for == and != are pretty useless

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Mon Jul 6 01:13:54 PDT 2015


https://llvm.org/bugs/show_bug.cgi?id=24036

            Bug ID: 24036
           Summary: sign-compare warning for == and != are pretty useless
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: Frontend
          Assignee: unassignedclangbugs at nondot.org
          Reporter: daniel.marjamaki at evidente.se
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

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) {}
}

Clang writes this warning:

signcompare.c:3:9: warning: comparison of integers of different signs: 'int'
and 'unsigned int' [-Wsign-compare]
  if (a == b) {}
      ~ ^  ~

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.

I believe this bug report is related to:
https://llvm.org/bugs/show_bug.cgi?id=24035

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20150706/05fe6638/attachment.html>


More information about the llvm-bugs mailing list