[cfe-dev] Static analyzer: possible memory leak false positive?

Tijl Coosemans tijl at coosemans.org
Fri Sep 28 04:37:22 PDT 2012


On 28-09-2012 08:51, Anna Zaks wrote:
> The analyzer specifically complains about memory allocated by malloc here:
> 196			/* just the filesystems specified on the command line */
> 197			mntbuf = malloc(argc * sizeof(*mntbuf));
> 
> So the call to free could be conditioned on the value of "!*argv" as well.

The process is exiting here, it shouldn't waste any time freeing memory.

Possible solution for this case:

- Have the analyzer treat return from main like it treats a call to exit(),
  at least by default (technically main could be called from another
  function).

- Have an analyzer_free() function which acts like free to the analyzer
  but is a no-op to the compiler. The experimental malloc checker supports
  this:

  static __inline__ __attribute__(( __always_inline__ ))
  __attribute__(( __ownership_takes__( __malloc__, 1 )))
  void
  analyzer_free( __attribute__(( __unused__ )) void *__p ) {
  }

  Then call analyzer_free(mntbuf) instead of free(mntbuf).

- Declare mntbuf static which tells the analyzer the memory is allowed to
  persist for the lifetime of the process.



More information about the cfe-dev mailing list