[cfe-commits] unix.Malloc static checker improvement: memory.LeakPtrValChanged

Jordan Rose jordan_rose at apple.com
Thu Jan 31 17:26:16 PST 2013


On Jan 31, 2013, at 5:21 , Branden Archer <b.m.archer4 at gmail.com> wrote:

> Basically, you need to pass a pointer which we are tracking to a function call indirectly (ex: as a field in a struct..). You should pass it to a function which is known not to free memory or close stream. Finally, you leak that resource/pointer.
> 
> Previously, we would have a false negative - no leak would be reported. Now, we should be catching the leak.
> 
> Ah, got you. See the first attached patch for these added cases.
> 
> - Branden


+void testPassConstPointerIndirectly() {
+  struct HasPtr hp;
+  hp.p = fopen("myfile.txt", "w");
+  fputc(0, (FILE *)&hp);
+  return; // expected-warning {{Opened file is never closed; potential resource leak}}
+}

Heh. Did you really want this test case? It's not actually valid (&hp is a FILE**, not a FILE*):


A few remaining comments for the MallocChecker patch:

+  if (ExplodedNode *N = C.generateSink()) {

Please use an early return here.


+    int offsetBytes = Offset.getOffset()/C.getASTContext().getCharWidth();

Very nitpicky, but can you put spaces around the /?


+       << ((abs(offsetBytes) > 1) ? "bytes" : "byte")

Perfect!

Jordan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130131/fd6c92a7/attachment.html>


More information about the cfe-commits mailing list