Menu ▾ ▴

False negative: memory leak through a pointer-to-pointer alias

Rodri
2026-09-29
2026-09-30
  • Rodri

    Rodri - 2026-09-29

    Hi, I found a false negative regarding the rule memleak.

    Cppcheck misses the leak when the allocated buffer is accessed through a pointer-to-pointer alias.

    #include <stdlib.h>
    
    int f(void) {
      int result = 0;
      char *a = malloc(10);
      char **b = &a;
      if (*b) {
        (*b)[0] = 0;
        result = (*b)[0];
      }
      return result;
    }
    

    Actual result

    Cppcheck does not report memleak. Removing b and replacing each *b access with a produces the warning.

    Expected result

    Cppcheck should report the missing free in both forms.

    Verification

    The issue was reproduced with:

    cppcheck --output-format=xmlv2 --enable=all malloc_alias.c
    

    Version: 2.21.0

     
  • CHR

    CHR - 2026-09-30

    Looks like a special case of https://trac.cppcheck.net/ticket/1606

     

Log in to post a comment.