Menu

FALSE POSITIVE: `zerodiv` reports inside an unreachable branch after a noreturn call

Rodri
4 days ago
4 days ago
  • Rodri

    Rodri - 4 days ago

    Hi, I found a false positive in Cppcheck 2.21.0 when a helper either calls a noreturn function or returns zero.

    Affected tool

    Cppcheck 2.21.0

    Affected checker

    Cppcheck zerodiv

    Minimal reproducer

    void stop(void) __attribute__((noreturn));
    
    int zero_or_stop(int condition) {
      if (condition)
        stop();
      return 0;
    }
    
    int unreachable_division(int condition) {
      int zero = 0;
      if (zero_or_stop(condition))
        return 1 / zero;
      return 0;
    }
    

    Reproduction command

    cppcheck --version
    cppcheck --enable=warning --std=c11 --checkers-report=checkers.txt --template='{file}:{line}:{column}: [{id}] {message}' cppcheck-zerodiv-fp-noreturn-summary.c
    

    Current behavior

    Cppcheck reports cppcheck-zerodiv-fp-noreturn-summary.c:12:14: [zerodiv] Division by zero.

    Expected behavior

    Cppcheck should not report zerodiv. zero_or_stop either does not return or returns zero, so the branch containing the division is unreachable.

     
  • CHR

    CHR - 4 days ago

    Looks like an instance of https://trac.cppcheck.net/ticket/14969
    There already is a condition attached to the error message.

     

Log in to post a comment.