Skip to content

Commit 47a419a

Browse files
committed
C++: Respond to review comments. First: Avoid using locations to detect constructor and destructor calls. Second: Include missing statements in stmtMayThrow.
1 parent 4463293 commit 47a419a

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

cpp/ql/src/experimental/Security/CWE/CWE-570/IncorrectAllocationErrorHandling.ql

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,20 @@ class DeleteOrDeleteArrayExpr extends Expr {
2323
DeallocationFunction getDeallocator() {
2424
result = [this.(DeleteExpr).getDeallocator(), this.(DeleteArrayExpr).getDeallocator()]
2525
}
26+
27+
Destructor getDestructor() {
28+
result = [this.(DeleteExpr).getDestructor(), this.(DeleteArrayExpr).getDestructor()]
29+
}
2630
}
2731

2832
/** Gets the `Constructor` invoked when `newExpr` allocates memory. */
2933
Constructor getConstructorForAllocation(NewOrNewArrayExpr newExpr) {
30-
result.getACallToThisFunction().getLocation() = newExpr.getLocation()
34+
result.getACallToThisFunction() = newExpr.getInitializer()
3135
}
3236

3337
/** Gets the `Destructor` invoked when `deleteExpr` deallocates memory. */
3438
Destructor getDestructorForDeallocation(DeleteOrDeleteArrayExpr deleteExpr) {
35-
result.getACallToThisFunction().getLocation() = deleteExpr.getLocation()
39+
result = deleteExpr.getDestructor()
3640
}
3741

3842
/** Holds if the evaluation of `newExpr` may throw an exception. */
@@ -63,15 +67,45 @@ predicate stmtMayThrow(Stmt stmt) {
6367
or
6468
convertedExprMayThrow(stmt.(ExprStmt).getExpr())
6569
or
70+
convertedExprMayThrow(stmt.(DeclStmt).getADeclaration().(Variable).getInitializer().getExpr())
71+
or
6672
exists(IfStmt ifStmt | ifStmt = stmt |
6773
convertedExprMayThrow(ifStmt.getCondition()) or
6874
stmtMayThrow([ifStmt.getThen(), ifStmt.getElse()])
6975
)
7076
or
77+
exists(ConstexprIfStmt constIfStmt | constIfStmt = stmt |
78+
stmtMayThrow([constIfStmt.getThen(), constIfStmt.getElse()])
79+
)
80+
or
7181
exists(Loop loop | loop = stmt |
7282
convertedExprMayThrow(loop.getCondition()) or
7383
stmtMayThrow(loop.getStmt())
7484
)
85+
or
86+
// The case for `Loop` already checked the condition and the statement.
87+
convertedExprMayThrow(stmt.(RangeBasedForStmt).getUpdate())
88+
or
89+
// The case for `Loop` already checked the condition and the statement.
90+
exists(ForStmt forStmt | forStmt = stmt |
91+
stmtMayThrow(forStmt.getInitialization())
92+
or
93+
convertedExprMayThrow(forStmt.getUpdate())
94+
)
95+
or
96+
exists(SwitchStmt switchStmt | switchStmt = stmt |
97+
convertedExprMayThrow(switchStmt.getExpr()) or
98+
stmtMayThrow(switchStmt.getStmt())
99+
)
100+
or
101+
stmtMayThrow(stmt.(SwitchCase).getAStmt())
102+
or
103+
// NOTE: We don't include `TryStmt` as those exceptions are not "observable" outside the function.
104+
stmtMayThrow(stmt.(Handler).getBlock())
105+
or
106+
convertedExprMayThrow(stmt.(CoReturnStmt).getExpr())
107+
or
108+
convertedExprMayThrow(stmt.(ReturnStmt).getExpr())
75109
}
76110

77111
/** Holds if the evaluation of `e` (including conversions) may throw an exception. */

0 commit comments

Comments
 (0)