Menu ▾ ▴

FALSE NEGATIVE: `mismatchAllocDealloc` misses an array allocation returned by a function

Rodri
2026-09-09
2026-09-09
  • Rodri

    Rodri - 2026-09-09

    Hi, I found a false negative in Cppcheck 2.22 dev when an array allocated with new[] is returned by a function and deallocated with delete.

    Affected tool

    Cppcheck 2.22 dev (Git commit 71de6753d64f)

    Affected checker

    Cppcheck mismatchAllocDealloc

    Minimal reproducer

    int *allocate_array() {
      return new int[1];
    }
    
    void release_array() {
      int *p = allocate_array();
      delete p;
    }
    

    Reproduction command

    cppcheck --version
    cppcheck --enable=warning --std=c++17 --checkers-report=checkers.txt --template='{file}:{line}:{column}: [{id}] {message}' cppcheck-mismatchallocdealloc-fn-returned-new-array.cpp
    

    Current behavior

    Cppcheck produces no mismatchAllocDealloc diagnostic. The checker report lists CheckLeakAutoVar::check as active.

    Expected behavior

    Cppcheck should report mismatchAllocDealloc at line 7 because the pointer returned by allocate_array() was allocated with new[] but is deallocated with delete.

     
  • CHR

    CHR - 2026-09-09
     

Log in to post a comment.