Menu

Should cppcheck flag comparing a uint32_t < 0u?

2018-11-06
2018-11-06
  • Richard Smith

    Richard Smith - 2018-11-06

    I found the following consturct in some code that is checked with cppcheck.

    uint32_t value = 0xFUL;
    
     <do things with value>
    
    if (value < 0u)
    {
        value = 0u;
    }
    

    The if() will never evaluate to true because its an unsigned comparison. I would have expected cppcheck to catch this but it does not seem to.

     
  • Daniel Marjamäki

    weird! There is something about the "0u" in your code. We do warn about this code:

    uint32_t value = 0xFUL;
    void f() {
      if (value < 0)
      {
        value = 0u;
      }
    }
    
     
    • Daniel Marjamäki

      I created https://trac.cppcheck.net/ticket/8836 ... I plan to look at it in the next couple of days.

       
  • Richard Smith

    Richard Smith - 2018-11-06

    I think something may be broken with my setup.

    I compiled and installed HEAD (43b6a391d) and created the file below.

    int main(void)
    {
        return f();
    }
    
    uint32_t value = 0xF;
    
    void f() {
        if (value < 0)
        {
            value = 0;
        }
    }
    

    but I don't get an error.

    rsmith@gatito:/home/src/cppcheck.git/ctest$ cppcheck main.c
    Checking main.c ...

     
  • Richard Smith

    Richard Smith - 2018-11-06

    Nevermind. I figured out that I need --enable=style. The MISRA checker reports the errors as style so I assume it was on by default. But I duplicated the 'u' causing a false negative.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.