Menu

FALSE NEGATIVE: `zerodiv` misses a zero from virtual base initialization

Rodri
2026-08-29
6 days ago
  • Rodri

    Rodri - 2026-08-29

    Hi, I found a false negative in Cppcheck 2.21.0 when the most-derived class default-initializes a virtual base member to zero.

    Affected checker

    Cppcheck zerodiv

    Minimal reproducer

    struct Base {
      int value;
      Base() : value(0) {}
      Base(int input) : value(input) {}
    };
    
    struct Derived : virtual Base {
      int result;
      Derived() : Base(1), result(1 / value) {}
    };
    
    struct MostDerived : Derived {};
    
    void construct() {
      MostDerived object;
    }
    

    Reproduction command

    cppcheck --version
    cppcheck --enable=warning --std=c++11 --checkers-report=checkers.txt --template='{file}:{line}:{column}: [{id}] {message}' cppcheck-zerodiv-fn-virtual-base-initializer.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 zerodiv at line 9. Constructing MostDerived invokes Base(), which sets the virtual base member value to zero before the Derived member initializer divides by it.

     

Log in to post a comment.