Menu

FALSE POSITIVE: `zerodiv` ignores a nonzero constraint through an alias

Rodri
4 days ago
4 days ago
  • Rodri

    Rodri - 4 days ago

    Hi, I found a false positive in Cppcheck 2.21.0 when equivalent pointer expressions constrain an aliased divisor to be nonzero.

    Affected tool

    Cppcheck 2.21.0

    Affected checker

    Cppcheck zerodiv

    Minimal reproducer

    int divide_after_alias_check(void *data) {
      int *value = (int *)data;
      *value = 0;
      if (*(int *)data == 0)
        return 0;
      return 1 / *value;
    }
    

    Reproduction command

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

    Current behavior

    Cppcheck reports cppcheck-zerodiv-fp-alias-constraint.c:6:12: [zerodiv] Division by zero.

    Expected behavior

    Cppcheck should not report zerodiv. The final return is reached only when *(int *)data != 0, and value points to that same object.

     
  • CHR

    CHR - 4 days ago

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

     

Log in to post a comment.