If you have a function that allocates and returns memory that is never intended to be freed, I recommend saving the returned pointer in a program-lifetime variable. This should make the tool think the memory might be freed by someone other than the caller, and hence not report memory leaks involving that allocator.
For example (not tested, I don't have access to the tool anymore):
#ifdef __COVERITY__
static void *dummy;
#endif
void *never_freed_malloc(size_t size)
{
void *ret = malloc(size);
#ifdef __COVERITY__
dummy = ret; /* suppress memory leak reports */
#endif
return ret;
}
With this change, Coverity sees that ret
"escapes" into the program-lifetime variable dummy
, and will therefore not assume that the caller must free it.
The #ifdef __COVERITY__
means that dummy
is only seen by the Coverity compiler, so won't affect run-time performance. See also Coverity. Configure to ignore certain sections of the source code.