Menu

FALSE NEGATIVE: Division-by-zero checking misses zero from a logical ternary

Rodri
2 days ago
1 day ago
  • Rodri

    Rodri - 2 days ago

    Hi, I found a false negative in Cppcheck 2.21.0 when a logical expression selects a zero-valued ternary branch.

    Affected tool

    Cppcheck 2.21.0

    Affected checker

    Cppcheck CheckOther::checkZeroDivision (zerodiv / zerodivcond)

    Minimal reproducer

    int divide_after_logical_ternary(bool left, bool right) {
      int divisor = (left || right) ? 0 : 1;
      return 1 / divisor;
    }
    

    Reproduction command

    cppcheck --version
    cppcheck --enable=warning --std=c++11 --checkers-report=checkers.txt --template='{file}:{line}:{column}: [{id}] {message}' cppcheck-zerodiv-fn-logical-ternary.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 a division-by-zero diagnostic at line 3 because either true operand selects the branch that assigns zero to divisor.

     
  • CHR

    CHR - 1 day ago

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

     

Log in to post a comment.