-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreturn-in-PG_TRY.ql
63 lines (56 loc) · 1.86 KB
/
return-in-PG_TRY.ql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* @name Find suspicious control flow stmt in PG_TRY()
* @kind problem
* @problem.severity warning
* @id postgresql/suspicious-control-flow-stmt-in-pg-try
*/
import cpp
predicate pgTryCatchBlocks(Stmt tryBlock, Stmt catchBlock) {
exists(IfStmt ifStmt, FunctionCall sigsetjmpCall, BinaryOperation op, Literal zero |
sigsetjmpCall.getTarget().hasName("__sigsetjmp") and
ifStmt.getCondition().(BinaryOperation) = op and
op.getOperator() = "==" and
op.hasOperands(sigsetjmpCall, zero) and
/* Reduce false positives. */
ifStmt.isAffectedByMacro() and
tryBlock = ifStmt.getThen() and
catchBlock = ifStmt.getElse()
)
}
predicate suspiciousReturn(Stmt stmt) { stmt instanceof ReturnStmt }
predicate suspiciousBreak(Stmt stmt, Stmt tryBlock) {
stmt instanceof BreakStmt and
not exists(Loop loop |
loop = tryBlock.getAChild+() and
loop.getAChild+() = stmt
) and
not exists(SwitchStmt switch |
switch = tryBlock.getAChild+() and
switch.getAChild+() = stmt
)
}
predicate suspiciousContinue(Stmt stmt, Stmt tryBlock) {
stmt instanceof ContinueStmt and
not exists(Loop loop |
loop = tryBlock.getAChild+() and
loop.getAChild+() = stmt
)
}
predicate suspiciousGoto(Stmt stmt, Stmt tryBlock) {
stmt instanceof GotoStmt and
not exists(LabelStmt label |
label.getName() = stmt.(GotoStmt).getName() and
label = tryBlock.getAChild+()
)
}
from Stmt tryBlock, Stmt suspiciousControlFlowStmt
where
pgTryCatchBlocks(tryBlock, _) and
suspiciousControlFlowStmt = tryBlock.getAChild*() and
(
suspiciousReturn(suspiciousControlFlowStmt) or
suspiciousBreak(suspiciousControlFlowStmt, tryBlock) or
suspiciousContinue(suspiciousControlFlowStmt, tryBlock) or
suspiciousGoto(suspiciousControlFlowStmt, tryBlock)
)
select suspiciousControlFlowStmt, "Found suspicious control flow statements in PG_TRY() block"