C++: Ignore free calls that are macro defined or #if/#ifdef guarded

This commit is contained in:
Jeroen Ketema
2024-11-14 12:51:04 +01:00
parent a31e983e9e
commit 176acabd9d
3 changed files with 16 additions and 7 deletions

View File

@@ -18,6 +18,17 @@ class FreeCall extends FunctionCall {
FreeCall() { this.getTarget().hasGlobalName("free") }
}
predicate isAffectedByMacro(FreeCall fc, BasicBlock bb) {
fc.isInMacroExpansion()
or
exists(PreprocessorBranch ppb, Location bbLoc, Location ppbLoc |
bbLoc = bb.(Stmt).getLocation() and ppbLoc = ppb.getLocation()
|
bbLoc.getStartLine() < ppbLoc.getStartLine() and
ppbLoc.getEndLine() < bbLoc.getEndLine()
)
}
from GuardCondition gc, FreeCall fc, Variable v, BasicBlock bb
where
gc.ensuresEq(v.getAnAccess(), 0, bb, false) and
@@ -28,5 +39,6 @@ where
or
strictcount(bb.(BlockStmt).getAStmt()) = 1
) and
strictcount(BasicBlock bb2 | gc.ensuresEq(_, 0, bb2, _) | bb2) = 1
strictcount(BasicBlock bb2 | gc.ensuresEq(_, 0, bb2, _) | bb2) = 1 and
not isAffectedByMacro(fc, bb)
select gc, "unnecessary NULL check before call to $@", fc, "free"