Skip to content

Commit 79ffedc

Browse files
kevinbackhouseneheb
authored andcommitted
Add CodeQL query to warn about null pointer exceptions in the print functions.
1 parent 6b186a4 commit 79ffedc

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @name Null metadata in print function
3+
* @description Print functions need to check that the metadata isn't null before calling methods on it.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @id cpp/null-metadata-in-print
7+
*/
8+
9+
import cpp
10+
import semmle.code.cpp.controlflow.Guards
11+
import semmle.code.cpp.controlflow.Nullness
12+
import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils
13+
14+
// Find all the print functions by looking for TagInfo initializers
15+
// like this one:
16+
// https://github.com/Exiv2/exiv2/blob/6b186a4cd276ac11b3ea69951c2112f4c4814b9a/src/canonmn_int.cpp#L660-L679
17+
class PrintFunction extends Function {
18+
PrintFunction() {
19+
exists(Initializer i, Field f |
20+
i.getExpr().(ArrayAggregateLiteral).getAChild().(ClassAggregateLiteral).getAFieldExpr(f) =
21+
this.getAnAccess() and
22+
f.getName() = "printFct_"
23+
)
24+
}
25+
}
26+
27+
from PrintFunction f, Parameter p, Call call, Expr qualifier
28+
where
29+
p = f.getParameter(2) and
30+
qualifier = p.getAnAccess() and
31+
call.getQualifier() = qualifier and
32+
// Don't complain if the access is protected by a null check.
33+
not exists(GuardCondition nonNullCheck, BasicBlock block, boolean branch |
34+
validCheckExpr(nonNullCheck, p) and
35+
nonNullCheck.controls(block, branch) and
36+
block.contains(call)
37+
)
38+
select qualifier, "Print functions need to check that the metadata isn't null."

0 commit comments

Comments
 (0)