Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6427e96

Browse files
committed
CPP: Remove successor edges from non-returning functions from the control flow graph.
1 parent 44f5e26 commit 6427e96

6 files changed

Lines changed: 91 additions & 74 deletions

File tree

cpp/ql/src/semmle/code/cpp/controlflow/internal/ConstantExprs.qll

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,25 @@ private predicate impossibleDefaultSwitchEdge(Block switchBlock, DefaultCase dc)
136136
val <= switchCaseRangeEnd(sc))))
137137
}
138138

139+
/**
140+
* Holds if the function `f` never returns due to not containing a return
141+
* statement (explicit or compiler generated). Reachability of return
142+
* statements is not checked.
143+
*/
144+
private predicate nonReturningFunction(Function f)
145+
{
146+
exists(f.getBlock()) and
147+
not exists(ReturnStmt ret | ret.getEnclosingFunction() = f)
148+
}
149+
150+
/**
151+
* An edge from a call to a function that never returns is impossible.
152+
*/
153+
private predicate impossibleFunctionReturn(FunctionCall fc, Node succ) {
154+
nonReturningFunction(fc.getTarget()) and
155+
successors_extended(fc, succ)
156+
}
157+
139158
/**
140159
* If `pred` is a function call with (at least) one function target,
141160
* (at least) one such target must be potentially returning.
@@ -158,6 +177,7 @@ cached predicate successors_adapted(Node pred, Node succ) {
158177
and not impossibleTrueEdge(pred, succ)
159178
and not impossibleSwitchEdge(pred, succ)
160179
and not impossibleDefaultSwitchEdge(pred, succ)
180+
and not impossibleFunctionReturn(pred, succ)
161181
and not getOptions().exprExits(pred)
162182
}
163183

cpp/ql/test/library-tests/c++_exceptions/graphable.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@
264264
| fun | true | 282 | 280 | |
265265
| fun | true | 284 | 290 | |
266266
| fun | true | 286 | 284 | |
267-
| fun | true | 288 | 297 | |
268267
| fun | true | 290 | 288 | |
269268
| fun | true | 294 | 295 | |
270269
| fun | true | 295 | 314 | |

cpp/ql/test/library-tests/c++_exceptions/unreachable.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
| ODASA-5692.cpp:11:18:13:3 | <handler> |
33
| ODASA-5692.cpp:14:15:15:12 | <handler> |
44
| ODASA-5692.cpp:14:15:15:12 | <handler> |
5+
| exceptions.cpp:25:13:30:9 | ExprStmt |
56
| exceptions.cpp:26:13:26:13 | ExprStmt |
7+
| exceptions.cpp:32:19:34:5 | <handler> |
8+
| exceptions.cpp:35:19:37:5 | <handler> |

cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/AV Rule 114.expected

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
| test.cpp:48:2:48:26 | if (...) ... | Function g7 should return a value of type MyValue but does not return a value here |
66
| test.cpp:74:1:76:1 | { ... } | Function g10 should return a value of type second but does not return a value here |
77
| test.cpp:86:1:88:1 | { ... } | Function g12 should return a value of type second but does not return a value here |
8-
| test.cpp:103:2:103:17 | ExprStmt | Function g13 should return a value of type int but does not return a value here |
98
| test.cpp:108:2:111:2 | if (...) ... | Function g14 should return a value of type int but does not return a value here |
10-
| test.cpp:110:3:110:18 | ExprStmt | Function g14 should return a value of type int but does not return a value here |
11-
| test.cpp:120:3:120:18 | ExprStmt | Function g15 should return a value of type int but does not return a value here |
129
| test.cpp:134:2:134:36 | ExprStmt | Function g16 should return a value of type int but does not return a value here |
1310
| test.cpp:141:3:141:37 | ExprStmt | Function g17 should return a value of type int but does not return a value here |
14-
| test.cpp:151:3:151:18 | ExprStmt | Function g18 should return a value of type int but does not return a value here |

cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void myThrow(const char *error)
100100

101101
int g13()
102102
{
103-
myThrow("fail"); // GOOD [FALSE POSITIVE]
103+
myThrow("fail"); // GOOD
104104
}
105105

106106
int g14(int x)
@@ -117,7 +117,7 @@ int g15(int x)
117117
{
118118
return x;
119119
} else {
120-
myThrow("fail"); // GOOD [FALSE POSITIVE]
120+
myThrow("fail"); // GOOD
121121
}
122122
}
123123

Lines changed: 66 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,66 @@
1-
| C::operator= | false | 129 | 129 | operator= |
2-
| C::operator= | false | 133 | 133 | operator= |
3-
| D::operator= | false | 140 | 140 | operator= |
4-
| D::operator= | false | 144 | 144 | operator= |
5-
| __va_list_tag::operator= | false | 61 | 61 | operator= |
6-
| __va_list_tag::operator= | false | 65 | 65 | operator= |
7-
| f | false | 163 | 163 | f |
8-
| f | false | 180 | 180 | call to g |
9-
| f | false | 182 | 182 | ExprStmt |
10-
| f | false | 186 | 186 | 2 |
11-
| f | false | 187 | 187 | throw ... |
12-
| f | false | 189 | 189 | ExprStmt |
13-
| f | false | 191 | 191 | label ...: |
14-
| f | false | 193 | 193 | { ... } |
15-
| f | false | 197 | 197 | 4 |
16-
| f | false | 198 | 198 | ExprStmt |
17-
| f | false | 200 | 200 | { ... } |
18-
| f | false | 202 | 202 | <handler> |
19-
| f | false | 203 | 203 | try { ... } |
20-
| f | false | 205 | 205 | { ... } |
21-
| f | false | 209 | 209 | 5 |
22-
| f | false | 210 | 210 | ExprStmt |
23-
| f | false | 212 | 212 | { ... } |
24-
| f | false | 216 | 216 | 6 |
25-
| f | false | 217 | 217 | ExprStmt |
26-
| f | false | 219 | 219 | { ... } |
27-
| f | false | 221 | 221 | <handler> |
28-
| f | false | 222 | 222 | <handler> |
29-
| f | false | 223 | 223 | try { ... } |
30-
| f | false | 225 | 225 | return ... |
31-
| f | false | 227 | 227 | { ... } |
32-
| f | true | 180 | 189 | |
33-
| f | true | 182 | 180 | |
34-
| f | true | 186 | 187 | |
35-
| f | true | 187 | 202 | |
36-
| f | true | 189 | 186 | |
37-
| f | true | 191 | 225 | |
38-
| f | true | 193 | 182 | |
39-
| f | true | 197 | 225 | |
40-
| f | true | 198 | 197 | |
41-
| f | true | 200 | 198 | |
42-
| f | true | 202 | 200 | |
43-
| f | true | 202 | 221 | |
44-
| f | true | 203 | 193 | |
45-
| f | true | 205 | 203 | |
46-
| f | true | 209 | 225 | |
47-
| f | true | 210 | 209 | |
48-
| f | true | 212 | 210 | |
49-
| f | true | 216 | 225 | |
50-
| f | true | 217 | 216 | |
51-
| f | true | 219 | 217 | |
52-
| f | true | 221 | 212 | |
53-
| f | true | 221 | 222 | |
54-
| f | true | 222 | 163 | |
55-
| f | true | 222 | 219 | |
56-
| f | true | 223 | 205 | |
57-
| f | true | 225 | 163 | |
58-
| f | true | 227 | 223 | |
59-
| g | false | 150 | 150 | g |
60-
| g | false | 155 | 155 | 1 |
61-
| g | false | 156 | 156 | throw ... |
62-
| g | false | 158 | 158 | ExprStmt |
63-
| g | false | 160 | 160 | { ... } |
64-
| g | true | 155 | 156 | |
65-
| g | true | 156 | 150 | |
66-
| g | true | 158 | 155 | |
67-
| g | true | 160 | 158 | |
1+
| C::operator= | false | 75 | 75 | operator= |
2+
| C::operator= | false | 81 | 81 | operator= |
3+
| D::operator= | false | 91 | 91 | operator= |
4+
| D::operator= | false | 97 | 97 | operator= |
5+
| __va_list_tag::operator= | false | 60 | 60 | operator= |
6+
| __va_list_tag::operator= | false | 66 | 66 | operator= |
7+
| f | false | 115 | 115 | f |
8+
| f | false | 120 | 120 | call to g |
9+
| f | false | 122 | 122 | ExprStmt |
10+
| f | false | 126 | 126 | 2 |
11+
| f | false | 127 | 127 | throw ... |
12+
| f | false | 129 | 129 | ExprStmt |
13+
| f | false | 131 | 131 | label ...: |
14+
| f | false | 133 | 133 | { ... } |
15+
| f | false | 141 | 141 | 4 |
16+
| f | false | 142 | 142 | ExprStmt |
17+
| f | false | 144 | 144 | { ... } |
18+
| f | false | 146 | 146 | <handler> |
19+
| f | false | 147 | 147 | try { ... } |
20+
| f | false | 149 | 149 | { ... } |
21+
| f | false | 157 | 157 | 5 |
22+
| f | false | 158 | 158 | ExprStmt |
23+
| f | false | 160 | 160 | { ... } |
24+
| f | false | 168 | 168 | 6 |
25+
| f | false | 169 | 169 | ExprStmt |
26+
| f | false | 171 | 171 | { ... } |
27+
| f | false | 173 | 173 | <handler> |
28+
| f | false | 174 | 174 | <handler> |
29+
| f | false | 175 | 175 | try { ... } |
30+
| f | false | 177 | 177 | return ... |
31+
| f | false | 179 | 179 | { ... } |
32+
| f | true | 122 | 120 | |
33+
| f | true | 126 | 127 | |
34+
| f | true | 127 | 146 | |
35+
| f | true | 129 | 126 | |
36+
| f | true | 131 | 177 | |
37+
| f | true | 133 | 122 | |
38+
| f | true | 141 | 177 | |
39+
| f | true | 142 | 141 | |
40+
| f | true | 144 | 142 | |
41+
| f | true | 146 | 144 | |
42+
| f | true | 146 | 173 | |
43+
| f | true | 147 | 133 | |
44+
| f | true | 149 | 147 | |
45+
| f | true | 157 | 177 | |
46+
| f | true | 158 | 157 | |
47+
| f | true | 160 | 158 | |
48+
| f | true | 168 | 177 | |
49+
| f | true | 169 | 168 | |
50+
| f | true | 171 | 169 | |
51+
| f | true | 173 | 160 | |
52+
| f | true | 173 | 174 | |
53+
| f | true | 174 | 115 | |
54+
| f | true | 174 | 171 | |
55+
| f | true | 175 | 149 | |
56+
| f | true | 177 | 115 | |
57+
| f | true | 179 | 175 | |
58+
| g | false | 102 | 102 | g |
59+
| g | false | 107 | 107 | 1 |
60+
| g | false | 108 | 108 | throw ... |
61+
| g | false | 110 | 110 | ExprStmt |
62+
| g | false | 112 | 112 | { ... } |
63+
| g | true | 107 | 108 | |
64+
| g | true | 108 | 102 | |
65+
| g | true | 110 | 107 | |
66+
| g | true | 112 | 110 | |

0 commit comments

Comments
 (0)