forked from Exiv2/exiv2
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CodeQL query to warn about null pointer exceptions in the print f…
…unctions.
- Loading branch information
1 parent
6b186a4
commit 37063e2
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
.github/codeql-queries/exiv2-cpp-queries/null_metadata_in_print.ql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* @name Null metadata in print function | ||
* @description Print functions need to check that the metadata isn't null before calling methods on it. | ||
* @kind problem | ||
* @problem.severity warning | ||
* @id cpp/null-metadata-in-print | ||
*/ | ||
|
||
import cpp | ||
import semmle.code.cpp.controlflow.Guards | ||
import semmle.code.cpp.controlflow.Nullness | ||
import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils | ||
|
||
// Find all the print functions by looking for TagInfo initializers | ||
// like this one: | ||
// https://github.com/Exiv2/exiv2/blob/6b186a4cd276ac11b3ea69951c2112f4c4814b9a/src/canonmn_int.cpp#L660-L679 | ||
class PrintFunction extends Function { | ||
PrintFunction() { | ||
exists(Initializer i, Field f | | ||
i.getExpr().(ArrayAggregateLiteral).getAChild().(ClassAggregateLiteral).getAFieldExpr(f) = | ||
this.getAnAccess() and | ||
f.getName() = "printFct_" | ||
) | ||
} | ||
} | ||
|
||
from PrintFunction f, Parameter p, Call call, Expr qualifier | ||
where | ||
p = f.getParameter(2) and | ||
qualifier = p.getAnAccess() and | ||
call.getQualifier() = qualifier and | ||
// Don't complain if the access is protected by a null check. | ||
not exists(GuardCondition nonNullCheck, BasicBlock block, boolean branch | | ||
validCheckExpr(nonNullCheck, p) and | ||
nonNullCheck.controls(block, branch) and | ||
block.contains(call) | ||
) | ||
select qualifier, "Print functions need to check that the metadata isn't null." |