Menu

FP Memleak when resource is passed to function call through a function pointer

2026-08-25
2026-08-27
  • Aaron Danen

    Aaron Danen - 2026-08-25

    Hello

    When analyzing this function with ./cppcheck test.c

    void f(void (*fptr)(void *)) {
      void *buf = malloc(1);
      (*fptr)(buf);
    }
    

    I get

    test.c:4:1: error: Memory leak: buf [memleak]
    }
    ^
    

    even though the call to whatever fptr points to might free or take ownership of buf.

    However, this similar function correctly reports no errors.

    void f() {
      void *buf = malloc(1);
      g(buf);
    }
    

    It is because in lib/checkleakautovar.cpp the "do we call a function" code only matches functions that are called with the standard "%name% (" syntax. I have a branch which modifies this check to handle function calls through function pointers, lambdas, and through functions that return function pointers like get_function()(buf). If it is helpful, I can make a PR once there is an issue.

    Best,
    Aaron

     
  • CHR

    CHR - 2026-08-27

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

     

Log in to post a comment.