Menu ▾ ▴

FALSE NEGATIVE: Division-by-zero checking misses a zero sum after two constraints

Rodri
2026-09-17
3 days ago
  • Rodri

    Rodri - 2026-09-17

    Hi, I found a false negative in Cppcheck 2.21.0 when two branch constraints establish that an expression used as a divisor is zero.

    Affected tool

    Cppcheck 2.21.0

    Affected checker

    Cppcheck CheckOther::checkZeroDivision (zerodiv / zerodivcond)

    Minimal reproducer

    int divide_after_two_constraints(int x, int y) {
      if (y != 0)
        return 0;
      if (x + y != 0)
        return 0;
      return y / (x + y);
    }
    

    Reproduction command

    cppcheck --version
    cppcheck --enable=warning --std=c++11 --checkers-report=checkers.txt --template='{file}:{line}:{column}: [{id}] {message}' cppcheck-zerodiv-fn-multiple-constraints.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 6 because reaching that statement establishes x + y == 0.

     
  • CHR

    CHR - 3 days ago

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

     

Log in to post a comment.