Skip to content

Commit 4c806a4

Browse files
committed
Swift: Dataflow through ? :.
1 parent 52d5578 commit 4c806a4

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

swift/ql/lib/codeql/swift/dataflow/internal/DataFlowPrivate.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ private module Cached {
167167
nodeTo.asExpr() = nco
168168
)
169169
or
170+
// flow through ternary operator `? :`
171+
exists(IfExpr ie |
172+
nodeTo.asExpr() = ie and
173+
(
174+
nodeFrom.asExpr() = ie.getThenExpr() or
175+
nodeFrom.asExpr() = ie.getElseExpr()
176+
)
177+
)
178+
or
170179
// flow through a flow summary (extension of `SummaryModelCsv`)
171180
FlowSummaryImpl::Private::Steps::summaryLocalStep(nodeFrom, nodeTo, true)
172181
}

swift/ql/test/library-tests/dataflow/dataflow/DataFlow.expected

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,15 @@ edges
105105
| test.swift:263:13:263:28 | call to optionalSource() : | test.swift:271:15:271:16 | ...? : |
106106
| test.swift:263:13:263:28 | call to optionalSource() : | test.swift:274:15:274:20 | ... ??(_:_:) ... |
107107
| test.swift:263:13:263:28 | call to optionalSource() : | test.swift:275:15:275:27 | ... ??(_:_:) ... |
108+
| test.swift:263:13:263:28 | call to optionalSource() : | test.swift:279:15:279:31 | ... ? ... : ... |
109+
| test.swift:263:13:263:28 | call to optionalSource() : | test.swift:280:15:280:38 | ... ? ... : ... |
108110
| test.swift:270:15:270:22 | call to source() : | file://:0:0:0:0 | [summary param] this in signum() : |
109111
| test.swift:270:15:270:22 | call to source() : | test.swift:270:15:270:31 | call to signum() |
110112
| test.swift:271:15:271:16 | ...? : | file://:0:0:0:0 | [summary param] this in signum() : |
111113
| test.swift:271:15:271:16 | ...? : | test.swift:271:15:271:25 | call to signum() : |
112114
| test.swift:271:15:271:25 | call to signum() : | test.swift:271:15:271:25 | OptionalEvaluationExpr |
115+
| test.swift:280:31:280:38 | call to source() : | test.swift:280:15:280:38 | ... ? ... : ... |
116+
| test.swift:282:31:282:38 | call to source() : | test.swift:282:15:282:38 | ... ? ... : ... |
113117
| test.swift:302:14:302:26 | (...) [Tuple element at index 1] : | test.swift:306:15:306:15 | t1 [Tuple element at index 1] : |
114118
| test.swift:302:18:302:25 | call to source() : | test.swift:302:14:302:26 | (...) [Tuple element at index 1] : |
115119
| test.swift:306:15:306:15 | t1 [Tuple element at index 1] : | test.swift:306:15:306:18 | .1 |
@@ -249,6 +253,11 @@ nodes
249253
| test.swift:271:15:271:25 | call to signum() : | semmle.label | call to signum() : |
250254
| test.swift:274:15:274:20 | ... ??(_:_:) ... | semmle.label | ... ??(_:_:) ... |
251255
| test.swift:275:15:275:27 | ... ??(_:_:) ... | semmle.label | ... ??(_:_:) ... |
256+
| test.swift:279:15:279:31 | ... ? ... : ... | semmle.label | ... ? ... : ... |
257+
| test.swift:280:15:280:38 | ... ? ... : ... | semmle.label | ... ? ... : ... |
258+
| test.swift:280:31:280:38 | call to source() : | semmle.label | call to source() : |
259+
| test.swift:282:15:282:38 | ... ? ... : ... | semmle.label | ... ? ... : ... |
260+
| test.swift:282:31:282:38 | call to source() : | semmle.label | call to source() : |
252261
| test.swift:302:14:302:26 | (...) [Tuple element at index 1] : | semmle.label | (...) [Tuple element at index 1] : |
253262
| test.swift:302:18:302:25 | call to source() : | semmle.label | call to source() : |
254263
| test.swift:306:15:306:15 | t1 [Tuple element at index 1] : | semmle.label | t1 [Tuple element at index 1] : |
@@ -332,6 +341,10 @@ subpaths
332341
| test.swift:271:15:271:25 | OptionalEvaluationExpr | test.swift:259:12:259:19 | call to source() : | test.swift:271:15:271:25 | OptionalEvaluationExpr | result |
333342
| test.swift:274:15:274:20 | ... ??(_:_:) ... | test.swift:259:12:259:19 | call to source() : | test.swift:274:15:274:20 | ... ??(_:_:) ... | result |
334343
| test.swift:275:15:275:27 | ... ??(_:_:) ... | test.swift:259:12:259:19 | call to source() : | test.swift:275:15:275:27 | ... ??(_:_:) ... | result |
344+
| test.swift:279:15:279:31 | ... ? ... : ... | test.swift:259:12:259:19 | call to source() : | test.swift:279:15:279:31 | ... ? ... : ... | result |
345+
| test.swift:280:15:280:38 | ... ? ... : ... | test.swift:259:12:259:19 | call to source() : | test.swift:280:15:280:38 | ... ? ... : ... | result |
346+
| test.swift:280:15:280:38 | ... ? ... : ... | test.swift:280:31:280:38 | call to source() : | test.swift:280:15:280:38 | ... ? ... : ... | result |
347+
| test.swift:282:15:282:38 | ... ? ... : ... | test.swift:282:31:282:38 | call to source() : | test.swift:282:15:282:38 | ... ? ... : ... | result |
335348
| test.swift:306:15:306:18 | .1 | test.swift:302:18:302:25 | call to source() : | test.swift:306:15:306:18 | .1 | result |
336349
| test.swift:317:15:317:18 | .0 | test.swift:314:12:314:19 | call to source() : | test.swift:317:15:317:18 | .0 | result |
337350
| test.swift:327:15:327:18 | .0 | test.swift:322:18:322:25 | call to source() : | test.swift:327:15:327:18 | .0 | result |

swift/ql/test/library-tests/dataflow/dataflow/LocalFlow.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,18 +216,26 @@
216216
| test.swift:279:15:279:15 | x | test.swift:280:15:280:15 | x |
217217
| test.swift:279:26:279:26 | x | test.swift:279:26:279:27 | ...! |
218218
| test.swift:279:26:279:26 | x | test.swift:280:15:280:15 | x |
219+
| test.swift:279:26:279:27 | ...! | test.swift:279:15:279:31 | ... ? ... : ... |
220+
| test.swift:279:31:279:31 | 0 | test.swift:279:15:279:31 | ... ? ... : ... |
219221
| test.swift:280:15:280:15 | x | test.swift:280:26:280:26 | x |
220222
| test.swift:280:15:280:15 | x | test.swift:284:16:284:16 | x |
221223
| test.swift:280:26:280:26 | x | test.swift:280:26:280:27 | ...! |
222224
| test.swift:280:26:280:26 | x | test.swift:284:16:284:16 | x |
225+
| test.swift:280:26:280:27 | ...! | test.swift:280:15:280:38 | ... ? ... : ... |
226+
| test.swift:280:31:280:38 | call to source() | test.swift:280:15:280:38 | ... ? ... : ... |
223227
| test.swift:281:15:281:15 | y | test.swift:281:26:281:26 | y |
224228
| test.swift:281:15:281:15 | y | test.swift:282:15:282:15 | y |
225229
| test.swift:281:26:281:26 | y | test.swift:281:26:281:27 | ...! |
226230
| test.swift:281:26:281:26 | y | test.swift:282:15:282:15 | y |
231+
| test.swift:281:26:281:27 | ...! | test.swift:281:15:281:31 | ... ? ... : ... |
232+
| test.swift:281:31:281:31 | 0 | test.swift:281:15:281:31 | ... ? ... : ... |
227233
| test.swift:282:15:282:15 | y | test.swift:282:26:282:26 | y |
228234
| test.swift:282:15:282:15 | y | test.swift:287:16:287:16 | y |
229235
| test.swift:282:26:282:26 | y | test.swift:282:26:282:27 | ...! |
230236
| test.swift:282:26:282:26 | y | test.swift:287:16:287:16 | y |
237+
| test.swift:282:26:282:27 | ...! | test.swift:282:15:282:38 | ... ? ... : ... |
238+
| test.swift:282:31:282:38 | call to source() | test.swift:282:15:282:38 | ... ? ... : ... |
231239
| test.swift:284:16:284:16 | x | test.swift:290:16:290:16 | x |
232240
| test.swift:287:16:287:16 | y | test.swift:293:16:293:16 | y |
233241
| test.swift:290:16:290:16 | x | test.swift:290:16:290:17 | ...? |

swift/ql/test/library-tests/dataflow/dataflow/test.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,10 @@ func test_optionals(y: Int?) {
276276
sink(arg: y ?? 0)
277277
sink(arg: y ?? source()) // $ MISSING: flow=278
278278

279-
sink(arg: x != nil ? x! : 0) // $ MISSING: flow=259
280-
sink(arg: x != nil ? x! : source()) // $ MISSING: flow=259, 281
279+
sink(arg: x != nil ? x! : 0) // $ flow=259
280+
sink(arg: x != nil ? x! : source()) // $ flow=259 flow=280
281281
sink(arg: y != nil ? y! : 0)
282-
sink(arg: y != nil ? y! : source()) // $ MISSING: flow=283
282+
sink(arg: y != nil ? y! : source()) // $ flow=282
283283

284284
if let z = x {
285285
sink(arg: z) // $ MISSING: flow=259

0 commit comments

Comments
 (0)