<div dir="ltr">Hi,<div>In case we have a code like -</div><div><br></div><div>int main() {</div><div>  char* p;</div><div>  delete p;  // Illegal Delete</div><div>}</div><div><br></div><div>Illegal delete for statement "delete p" is not reported. Added a patch to fix the same. Please let me know if the patch is ok.</div>
<div><br></div><div><div>Index: test/Analysis/malloc-interprocedural.c</div><div>===================================================================</div><div>--- test/Analysis/malloc-interprocedural.c<span class="" style="white-space:pre">        </span>(revision 187647)</div>
<div>+++ test/Analysis/malloc-interprocedural.c<span class="" style="white-space:pre">  </span>(working copy)</div><div>@@ -68,10 +68,13 @@</div><div>   my_free1((int*)data); // expected-warning{{Use of memory after it is freed}}</div>
<div> }</div><div> </div><div>+static void my_free2(void *p) {</div><div>+  free(p); // expected-warning{{Freeing a non allocated memory}}</div><div>+}</div><div> // TODO: We should warn here.</div><div> void test5() {</div>
<div>   int *data;</div><div>-  my_free1((int*)data);</div><div>+  my_free2((int*)data);</div><div> }</div><div> </div><div> static char *reshape(char *in) {</div><div>Index: lib/StaticAnalyzer/Checkers/MallocChecker.cpp</div>
<div>===================================================================</div><div>--- lib/StaticAnalyzer/Checkers/MallocChecker.cpp<span class="" style="white-space:pre"> </span>(revision 187648)</div><div>+++ lib/StaticAnalyzer/Checkers/MallocChecker.cpp<span class="" style="white-space:pre"> </span>(working copy)</div>
<div>@@ -961,8 +961,19 @@</div><div>                                           bool ReturnsNullOnFailure) const {</div><div> </div><div>   SVal ArgVal = State->getSVal(ArgExpr, C.getLocationContext());</div><div>-  if (!ArgVal.getAs<DefinedOrUnknownSVal>())</div>
<div>+  if (!ArgVal.getAs<DefinedOrUnknownSVal>()) {</div><div>+    if (ExplodedNode *N = C.addTransition(C.getState())) {</div><div>+      if (!BT_BadFree)</div><div>+        BT_BadFree.reset(new BugType("Bad free", "Memory Error"));</div>
<div>+      SmallString<100> buf;</div><div>+      llvm::raw_svector_ostream os(buf);</div><div>+      os << "Freeing a non allocated memory";</div><div>+      BugReport *R = new BugReport(*BT_BadFree, os.str(), N);</div>
<div>+      R->addRange(ArgExpr->getSourceRange());</div><div>+      C.emitReport(R);</div><div>+    }</div><div>     return 0;</div><div>+  }</div><div>   DefinedOrUnknownSVal location = ArgVal.castAs<DefinedOrUnknownSVal>();</div>
</div><div><br></div><div><br></div><div>Thanks and Regards</div><div>Karthik Bhat</div></div>