Menu

False positive when incrementing unsigned integer

nc
2025-01-06
2025-01-07
  • nc

    nc - 2025-01-06

    I have encountered a false positive in Cppcheck 2.13.0

    Severity: style
    Summary: Condition '++counter==0' is always false

    Example code that triggers the false positive:

    #include <iostream>
    #include <limits>
    
    void incr(unsigned & counter)
    {
        std::cout << "Incrementing counter. Curr value: " << counter << '\n';
    
        if (++counter == 0)
        {
            std::cout << "Condition '++counter == 0' is true\n";
        }
    
        std::cout << "Value after increment: " << counter << '\n';
    }
    
    int main()
    {
        unsigned counter {};
        incr(counter);
    
        counter = std::numeric_limits<unsigned>::max();
        incr(counter);
    }
    
     
  • CHR

    CHR - 2025-01-07

    Looks like an instance of this issue: https://trac.cppcheck.net/ticket/10616

     

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.