Menu

"Condition is always true" false positive

2023-01-30
2023-02-03
  • Nikita Leontiev

    Nikita Leontiev - 2023-01-30

    Below "Condition is always true" false positive occurs since cppcheck 2.10:

    #include <inttypes.h>
    #include <vector>
    
    void func(const std::vector <uint8_t> & bytes)
    {
        size_t size = bytes.size();
        size_t count = (size > 100)?100:size;
        if (count < 100)
        {
        }
    }
    
    int main()
    {
        return 0;
    }
    
    [test.cpp:8]: (style) Condition 'count<100' is always true
    
     
  • CHR

    CHR - 2023-01-31

    Thanks for reporting, I think this is covered by https://trac.cppcheck.net/ticket/11437

     
  • Tobias Mark

    Tobias Mark - 2023-02-02

    Here another false-positive (never seen this with 2.9.) that's maybe related:

    int func(unsigned char c) {
        if(c>127 && c<160) {
            int x = 0;
            if(c == 128) {
                x = 1;
            }
            if(x != 0) {
                return 1;
            }
        }
        return 0;
    }
    
    test.c:18:7: note: Condition 'x!=0' is always false
     if(x != 0) {
    

    When removing the c<160 condition the error goes away.

     
    • CHR

      CHR - 2023-02-02

      That seems to be a different issue, ticket: https://trac.cppcheck.net/ticket/11532

       
  • Andrew C Aitchison

    Can you confirm that 2.9 does not give this error ?
    My test suggest that cppcheck --enable=style gives this problem at least at far back as 2.6 for Tobias's code with c<160

     

    Last edit: Andrew C Aitchison 2023-02-02
  • Tobias Mark

    Tobias Mark - 2023-02-03

    I can't confirm this.
    It may be possible that some internalASTError (that are no fixed) prevented this error for me with 2.9.

     

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.