Package: cppcheck Version: 1.44-1 Things like the following cause cppcheck to report a memory leak where there is none (whole example generated from the preprocessed file is attachment):
retvalue f(const char *c, const char *fallback, char **s_p) { retvalue r; char *s; r = g(c, &s); /* sets s if return value > 0 */ if( r == RET_NOTHING ) { s = strdup(fallback); if( s == ((void *)0) ) r = RET_ERROR_OOM; } if( r < 0 ) return r; /* <- cppcheck reports: [test2.c:22]: (error) Memory leak: s */ *s_p = s; return RET_OK; } Also found with 68beffca043ba23114824ed46408e65cb0d653ec Bernhard R. Link
extern char *strdup (__const char *__s) __attribute__ ((__nothrow__)) __attribute__ ((__malloc__)) __attribute__ ((__nonnull__ (1))); enum retvalue_enum { RET_ERROR_OOM = -3, RET_ERROR = -1, RET_NOTHING = 0, RET_OK = 1 }; typedef enum retvalue_enum retvalue; retvalue g(const char *, char **); retvalue f(const char *c, const char *fallback, char **s_p) { retvalue r; char *s; r = g(c, &s); /* sets s if return value > 0 */ if( r == RET_NOTHING ) { s = strdup(fallback); if( s == ((void *)0) ) r = RET_ERROR_OOM; } if( r < 0 ) return r; /* <- cppcheck reports: [test2.c:22]: (error) Memory leak: s */ *s_p = s; return RET_OK; }