I have a library that allocates objects. In most cases these objects are never freed by the applications. Naturally, Coverity flags each and every use of this library with leaked_storage.
I really do not want to add 100s of Coverity annotations, is it possible to suppress these warnings from inside the library?
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.