forked from github/codeql-coding-standards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFgetsErrorManagement.qll
More file actions
169 lines (150 loc) · 4.32 KB
/
FgetsErrorManagement.qll
File metadata and controls
169 lines (150 loc) · 4.32 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* A module for reasoning about the error management in the standard functions
* `fgets`, `fgets` and their wrappers.
*/
import cpp
private import semmle.code.cpp.dataflow.DataFlow
import semmle.code.cpp.controlflow.Guards
/*
* Models calls to fgets fgetws and their wrappers
*/
class FgetsLikeCall extends FunctionCall {
Expr buffer;
Expr stream;
FgetsLikeCall() {
this.getTarget().hasGlobalName(["fgets", "fgetws"]) and
buffer = this.getArgument(0) and
stream = this.getArgument(2)
or
exists(FgetsLikeCall internalCall, ReturnStmt retStmt, int buffer_pos, int stream_pos |
internalCall.getEnclosingFunction() = this.getTarget() and
retStmt.getEnclosingFunction() = this.getTarget() and
// Map return value
DataFlow::localExprFlow(internalCall, retStmt.getExpr()) and
// Map buffer argument
buffer = this.getArgument(buffer_pos) and
DataFlow::localFlow(DataFlow::parameterNode(this.getTarget().getParameter(buffer_pos)),
DataFlow::exprNode(internalCall.getBuffer())) and
// Map stream argument
stream = this.getArgument(stream_pos) and
DataFlow::localFlow(DataFlow::parameterNode(this.getTarget().getParameter(stream_pos)),
DataFlow::exprNode(internalCall.getStream()))
)
}
Expr getBuffer() { result = buffer }
Expr getStream() { result = stream }
}
/*
* A simple boolean expression built on a fgets-like function call
* The boolean expression can span multiple statments
* (e.g. a = !x; b=!a;)
*/
class BooleanFgetsExpr extends Expr {
boolean isNull;
boolean isNotNull;
FgetsLikeCall fgetCall;
Expr operand;
BooleanFgetsExpr() {
// if(fgets)
fgetCall = this and
isNull = false and
isNotNull = true and
operand = this
or
exists(BooleanFgetsExpr e |
e != this and
operand = e and
fgetCall = e.getFgetCall() and
(
isNotNull = isNull.booleanNot() and
(
// if(e==0)
e = this.(EQExpr).getAnOperand() and
this.(EQExpr).getAnOperand() instanceof NullValue and
isNull = e.isNull().booleanNot()
or
// if(e!=0)
e = this.(NEExpr).getAnOperand() and
this.(NEExpr).getAnOperand() instanceof NullValue and
isNull = e.isNull()
or
// if(e)
DataFlow::localExprFlow(e, this) and
isNull = e.isNull()
or
// if(!e)
e = this.(NotExpr).getOperand() and
isNull = e.isNull().booleanNot()
or
// if(cond && e)
e = this.(LogicalAndExpr).getRightOperand() and
isNull = e.isNull()
or
// if(cond || e)
e = this.(LogicalOrExpr).getRightOperand() and
isNull = e.isNull()
)
or
// if(e && cond)
e = this.(LogicalAndExpr).getLeftOperand() and
(
isNull = false and
isNotNull = false
)
or
// if(e || cond)
e = this.(LogicalOrExpr).getLeftOperand() and
(
isNull = true and
isNotNull = true
)
)
)
}
boolean isNull() { result = isNull }
boolean isNotNull() { result = isNotNull }
Expr getFgetCall() { result = fgetCall }
Expr getOperand() { result = operand }
}
/*
* A guard controlled by a `BooleanFgetsExpr`
*/
class FgetsGuard extends BooleanFgetsExpr {
FgetsGuard() {
exists(IfStmt i | i.getCondition() = this)
or
exists(Loop i | i.getCondition() = this)
}
Stmt getThenSuccessor() {
exists(IfStmt i | i.getCondition() = this and result = i.getThen())
or
exists(Loop i | i.getCondition() = this and result = i.getStmt())
}
Stmt getElseSuccessor() {
exists(IfStmt i |
i.getCondition() = this and
(
i.hasElse() and result = i.getElse()
or
not i.hasElse() and result = i.getFollowingStmt()
)
)
or
exists(Loop i |
i.getCondition() = this and
result = i.getFollowingStmt()
)
}
ControlFlowNode getNullSuccessor() {
this.isNull() = true and result = this.getThenSuccessor()
or
this.isNull() = false and result = getElseSuccessor()
}
ControlFlowNode getNonNullSuccessor() {
this.isNotNull() = true and
result = this.getThenSuccessor()
or
this.isNotNull() = false and
result = getElseSuccessor()
}
}