Menu

FALSE NEGATIVE: zerodiv misses a known zero divisor in a function template with an unknown branch condition

Rodri
2 days ago
1 day ago
  • Rodri

    Rodri - 2 days ago

    Hi, I found a false negative in Cppcheck 2.21.0 and 2.22 dev when a constant argument makes a division inside a function-template instantiation divide by zero.

    Affected tool

    Cppcheck 2.21.0; also reproduced with Cppcheck 2.22 dev

    Affected checker

    Cppcheck CheckOther::checkZeroDivision (zerodiv / zerodivcond)

    Minimal reproducer

    bool unknown();
    
    template <class T>
    void divide(T value) {
      if (unknown())
        value = 1 / (value - 5);
    }
    
    void test() {
      divide<int>(5);
    }
    

    Reproduction command

    cppcheck --version
    cppcheck --enable=warning --std=c++17 --checkers-report=checkers.txt --template='{file}:{line}:{column}: [{id}] {message}' cppcheck-zerodiv-fn-template-unknown-branch.cpp
    

    Current behavior

    Cppcheck produces neither a zerodiv nor a zerodivcond diagnostic. The checker report lists CheckOther::checkZeroDivision as active.

    Expected behavior

    Cppcheck should report line 6 because the instantiation receives value == 5, making value - 5 equal to zero.

     
  • CHR

    CHR - 1 day ago

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/15051

     

Log in to post a comment.