Menu

False Negative. Does not create a warning in comparison such as: "unsigned int < 0"

2020-02-12
2020-02-17
  • Gal Zimmerman

    Gal Zimmerman - 2020-02-12

    When comparing "unsigned int < 0", it is expected to get a warning that expression always evaluated to false.

     
  • Daniel Marjamäki

    Works for me:

    $ ./cppcheck --enable=style file.c 
    Checking file.c ...
    file.c:12:10: style: Condition 'u<0' is always false [knownConditionTrueFalse]
        if (u<0)
             ^
    file.c:11:24: note: Assignment 'u=f()', assigned value is 4294967295
        unsigned int  u = f();
                           ^
    file.c:12:10: note: Condition 'u<0' is always false
        if (u<0)
             ^
    file.c:12:10: style: Checking if unsigned expression 'u' is less than zero. [unsignedLessThanZero]
        if (u<0)
             ^
    
     
  • Gal Zimmerman

    Gal Zimmerman - 2020-02-13

    This is nice.
    You are right, we running Cppcheck without Style messages and thus did not get this those messages.
    However, for me, it does not look like a style issue for me. It is a real bug that must be fixed.

     
  • Gal Zimmerman

    Gal Zimmerman - 2020-02-13

    This is nice.
    You are right, we running Cppcheck without Style messages and thus did not get this those messages.
    However, for me, it does not look like a style issue for me. It is a real bug that must be fixed.

     
  • Daniel Marjamäki

    For cppcheck it's hard to determine if such comparisons are bugs or written like that by intention.

    We only use warning/error severities if the code might have undefined behavior and that is not the case here. So well in Cppcheck, the motivation for most style checks are that there could be some bug.

     
  • Gal Zimmerman

    Gal Zimmerman - 2020-02-17

    Thanks for the response and explanations.
    However, I added 'unsignedLessThanZero' check on my code and in most of cases it notifies the message on comparison such as 'u<= 0'.
    I agree that 'u<= 0' is really a style.
    IMO 'u<0' is different from 'u<=0' and it should be a warning message.

     

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.