Skip to content

Commit 2dbb891

Browse files
committed
Swift: Dataflow through ??.
1 parent 9b38e11 commit 2dbb891

File tree

4 files changed

+62
-41
lines changed

4 files changed

+62
-41
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ private module Cached {
153153
or
154154
nodeFrom.asExpr() = nodeTo.asExpr().(OptionalEvaluationExpr).getSubExpr()
155155
or
156+
// flow through nil-coalescing operator `??`
157+
exists(BinaryExpr nco |
158+
nco.getFunction().(DeclRefExpr).getDecl().(FreeFunctionDecl).getName() = "??(_:_:)" and
159+
nodeFrom.asExpr() = nco.getAnOperand() and
160+
nodeTo.asExpr() = nco
161+
)
162+
or
156163
// flow through a flow summary (extension of `SummaryModelCsv`)
157164
FlowSummaryImpl::Private::Steps::summaryLocalStep(nodeFrom, nodeTo, true)
158165
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ edges
103103
| test.swift:263:13:263:28 | call to optionalSource() : | test.swift:265:15:265:15 | x |
104104
| test.swift:263:13:263:28 | call to optionalSource() : | test.swift:267:15:267:16 | ...! |
105105
| test.swift:263:13:263:28 | call to optionalSource() : | test.swift:271:15:271:16 | ...? : |
106+
| test.swift:263:13:263:28 | call to optionalSource() : | test.swift:274:15:274:20 | ... ??(_:_:) ... |
107+
| test.swift:263:13:263:28 | call to optionalSource() : | test.swift:275:15:275:27 | ... ??(_:_:) ... |
106108
| test.swift:270:15:270:22 | call to source() : | file://:0:0:0:0 | [summary param] this in signum() : |
107109
| test.swift:270:15:270:22 | call to source() : | test.swift:270:15:270:31 | call to signum() |
108110
| test.swift:271:15:271:16 | ...? : | file://:0:0:0:0 | [summary param] this in signum() : |
@@ -245,6 +247,8 @@ nodes
245247
| test.swift:271:15:271:16 | ...? : | semmle.label | ...? : |
246248
| test.swift:271:15:271:25 | OptionalEvaluationExpr | semmle.label | OptionalEvaluationExpr |
247249
| test.swift:271:15:271:25 | call to signum() : | semmle.label | call to signum() : |
250+
| test.swift:274:15:274:20 | ... ??(_:_:) ... | semmle.label | ... ??(_:_:) ... |
251+
| test.swift:275:15:275:27 | ... ??(_:_:) ... | semmle.label | ... ??(_:_:) ... |
248252
| test.swift:302:14:302:26 | (...) [Tuple element at index 1] : | semmle.label | (...) [Tuple element at index 1] : |
249253
| test.swift:302:18:302:25 | call to source() : | semmle.label | call to source() : |
250254
| test.swift:306:15:306:15 | t1 [Tuple element at index 1] : | semmle.label | t1 [Tuple element at index 1] : |
@@ -326,6 +330,8 @@ subpaths
326330
| test.swift:267:15:267:16 | ...! | test.swift:259:12:259:19 | call to source() : | test.swift:267:15:267:16 | ...! | result |
327331
| test.swift:270:15:270:31 | call to signum() | test.swift:270:15:270:22 | call to source() : | test.swift:270:15:270:31 | call to signum() | result |
328332
| test.swift:271:15:271:25 | OptionalEvaluationExpr | test.swift:259:12:259:19 | call to source() : | test.swift:271:15:271:25 | OptionalEvaluationExpr | result |
333+
| test.swift:274:15:274:20 | ... ??(_:_:) ... | test.swift:259:12:259:19 | call to source() : | test.swift:274:15:274:20 | ... ??(_:_:) ... | result |
334+
| test.swift:275:15:275:27 | ... ??(_:_:) ... | test.swift:259:12:259:19 | call to source() : | test.swift:275:15:275:27 | ... ??(_:_:) ... | result |
329335
| test.swift:306:15:306:18 | .1 | test.swift:302:18:302:25 | call to source() : | test.swift:306:15:306:18 | .1 | result |
330336
| test.swift:317:15:317:18 | .0 | test.swift:314:12:314:19 | call to source() : | test.swift:317:15:317:18 | .0 | result |
331337
| 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: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,18 @@
200200
| test.swift:272:15:272:15 | y | test.swift:272:15:272:16 | ...? |
201201
| test.swift:272:15:272:15 | y | test.swift:276:15:276:15 | y |
202202
| test.swift:272:15:272:25 | call to signum() | test.swift:272:15:272:25 | OptionalEvaluationExpr |
203+
| test.swift:274:15:274:15 | x | test.swift:274:15:274:20 | ... ??(_:_:) ... |
203204
| test.swift:274:15:274:15 | x | test.swift:275:15:275:15 | x |
205+
| test.swift:274:20:274:20 | { ... } | test.swift:274:15:274:20 | ... ??(_:_:) ... |
206+
| test.swift:275:15:275:15 | x | test.swift:275:15:275:27 | ... ??(_:_:) ... |
204207
| test.swift:275:15:275:15 | x | test.swift:279:15:279:15 | x |
208+
| test.swift:275:20:275:27 | { ... } | test.swift:275:15:275:27 | ... ??(_:_:) ... |
209+
| test.swift:276:15:276:15 | y | test.swift:276:15:276:20 | ... ??(_:_:) ... |
205210
| test.swift:276:15:276:15 | y | test.swift:277:15:277:15 | y |
211+
| test.swift:276:20:276:20 | { ... } | test.swift:276:15:276:20 | ... ??(_:_:) ... |
212+
| test.swift:277:15:277:15 | y | test.swift:277:15:277:27 | ... ??(_:_:) ... |
206213
| test.swift:277:15:277:15 | y | test.swift:281:15:281:15 | y |
214+
| test.swift:277:20:277:27 | { ... } | test.swift:277:15:277:27 | ... ??(_:_:) ... |
207215
| test.swift:279:15:279:15 | x | test.swift:279:26:279:26 | x |
208216
| test.swift:279:15:279:15 | x | test.swift:280:15:280:15 | x |
209217
| test.swift:279:26:279:26 | x | test.swift:279:26:279:27 | ...! |
@@ -226,44 +234,44 @@
226234
| test.swift:290:16:290:26 | call to signum() | test.swift:290:16:290:26 | OptionalEvaluationExpr |
227235
| test.swift:293:16:293:16 | y | test.swift:293:16:293:17 | ...? |
228236
| test.swift:293:16:293:26 | call to signum() | test.swift:293:16:293:26 | OptionalEvaluationExpr |
229-
| test.swift:304:9:304:9 | WriteDef | test.swift:306:15:306:15 | t1 |
230-
| test.swift:304:14:304:26 | (...) | test.swift:304:9:304:9 | WriteDef |
231-
| test.swift:306:15:306:15 | t1 | test.swift:307:15:307:15 | t1 |
232-
| test.swift:307:15:307:15 | [post] t1 | test.swift:308:15:308:15 | t1 |
233-
| test.swift:307:15:307:15 | t1 | test.swift:308:15:308:15 | t1 |
234-
| test.swift:308:15:308:15 | [post] t1 | test.swift:310:5:310:5 | t1 |
235-
| test.swift:308:15:308:15 | t1 | test.swift:310:5:310:5 | t1 |
236-
| test.swift:310:5:310:5 | [post] t1 | test.swift:312:15:312:15 | t1 |
237-
| test.swift:310:5:310:5 | t1 | test.swift:312:15:312:15 | t1 |
238-
| test.swift:312:15:312:15 | t1 | test.swift:313:15:313:15 | t1 |
239-
| test.swift:313:15:313:15 | [post] t1 | test.swift:314:15:314:15 | t1 |
240-
| test.swift:313:15:313:15 | t1 | test.swift:314:15:314:15 | t1 |
241-
| test.swift:314:15:314:15 | [post] t1 | test.swift:316:5:316:5 | t1 |
242-
| test.swift:314:15:314:15 | t1 | test.swift:316:5:316:5 | t1 |
243-
| test.swift:316:5:316:5 | [post] t1 | test.swift:318:15:318:15 | t1 |
244-
| test.swift:316:5:316:5 | t1 | test.swift:318:15:318:15 | t1 |
245-
| test.swift:318:15:318:15 | t1 | test.swift:319:15:319:15 | t1 |
246-
| test.swift:319:15:319:15 | [post] t1 | test.swift:320:15:320:15 | t1 |
247-
| test.swift:319:15:319:15 | t1 | test.swift:320:15:320:15 | t1 |
248-
| test.swift:324:9:324:9 | WriteDef | test.swift:325:14:325:14 | t1 |
249-
| test.swift:324:14:324:45 | (...) | test.swift:324:9:324:9 | WriteDef |
250-
| test.swift:325:9:325:9 | WriteDef | test.swift:332:15:332:15 | t2 |
251-
| test.swift:325:14:325:14 | t1 | test.swift:325:9:325:9 | WriteDef |
252-
| test.swift:325:14:325:14 | t1 | test.swift:326:21:326:21 | t1 |
253-
| test.swift:326:9:326:17 | WriteDef | test.swift:336:15:336:15 | a |
254-
| test.swift:326:9:326:17 | WriteDef | test.swift:337:15:337:15 | b |
255-
| test.swift:326:9:326:17 | WriteDef | test.swift:338:15:338:15 | c |
256-
| test.swift:326:21:326:21 | t1 | test.swift:326:9:326:17 | WriteDef |
257-
| test.swift:326:21:326:21 | t1 | test.swift:326:9:326:17 | WriteDef |
258-
| test.swift:326:21:326:21 | t1 | test.swift:326:9:326:17 | WriteDef |
259-
| test.swift:326:21:326:21 | t1 | test.swift:328:15:328:15 | t1 |
237+
| test.swift:302:9:302:9 | WriteDef | test.swift:304:15:304:15 | t1 |
238+
| test.swift:302:14:302:26 | (...) | test.swift:302:9:302:9 | WriteDef |
239+
| test.swift:304:15:304:15 | t1 | test.swift:305:15:305:15 | t1 |
240+
| test.swift:305:15:305:15 | [post] t1 | test.swift:306:15:306:15 | t1 |
241+
| test.swift:305:15:305:15 | t1 | test.swift:306:15:306:15 | t1 |
242+
| test.swift:306:15:306:15 | [post] t1 | test.swift:308:5:308:5 | t1 |
243+
| test.swift:306:15:306:15 | t1 | test.swift:308:5:308:5 | t1 |
244+
| test.swift:308:5:308:5 | [post] t1 | test.swift:310:15:310:15 | t1 |
245+
| test.swift:308:5:308:5 | t1 | test.swift:310:15:310:15 | t1 |
246+
| test.swift:310:15:310:15 | t1 | test.swift:311:15:311:15 | t1 |
247+
| test.swift:311:15:311:15 | [post] t1 | test.swift:312:15:312:15 | t1 |
248+
| test.swift:311:15:311:15 | t1 | test.swift:312:15:312:15 | t1 |
249+
| test.swift:312:15:312:15 | [post] t1 | test.swift:314:5:314:5 | t1 |
250+
| test.swift:312:15:312:15 | t1 | test.swift:314:5:314:5 | t1 |
251+
| test.swift:314:5:314:5 | [post] t1 | test.swift:316:15:316:15 | t1 |
252+
| test.swift:314:5:314:5 | t1 | test.swift:316:15:316:15 | t1 |
253+
| test.swift:316:15:316:15 | t1 | test.swift:317:15:317:15 | t1 |
254+
| test.swift:317:15:317:15 | [post] t1 | test.swift:318:15:318:15 | t1 |
255+
| test.swift:317:15:317:15 | t1 | test.swift:318:15:318:15 | t1 |
256+
| test.swift:322:9:322:9 | WriteDef | test.swift:323:14:323:14 | t1 |
257+
| test.swift:322:14:322:45 | (...) | test.swift:322:9:322:9 | WriteDef |
258+
| test.swift:323:9:323:9 | WriteDef | test.swift:330:15:330:15 | t2 |
259+
| test.swift:323:14:323:14 | t1 | test.swift:323:9:323:9 | WriteDef |
260+
| test.swift:323:14:323:14 | t1 | test.swift:324:21:324:21 | t1 |
261+
| test.swift:324:9:324:17 | WriteDef | test.swift:334:15:334:15 | a |
262+
| test.swift:324:9:324:17 | WriteDef | test.swift:335:15:335:15 | b |
263+
| test.swift:324:9:324:17 | WriteDef | test.swift:336:15:336:15 | c |
264+
| test.swift:324:21:324:21 | t1 | test.swift:324:9:324:17 | WriteDef |
265+
| test.swift:324:21:324:21 | t1 | test.swift:324:9:324:17 | WriteDef |
266+
| test.swift:324:21:324:21 | t1 | test.swift:324:9:324:17 | WriteDef |
267+
| test.swift:324:21:324:21 | t1 | test.swift:326:15:326:15 | t1 |
268+
| test.swift:326:15:326:15 | t1 | test.swift:327:15:327:15 | t1 |
269+
| test.swift:327:15:327:15 | [post] t1 | test.swift:328:15:328:15 | t1 |
270+
| test.swift:327:15:327:15 | t1 | test.swift:328:15:328:15 | t1 |
271+
| test.swift:328:15:328:15 | [post] t1 | test.swift:329:15:329:15 | t1 |
260272
| test.swift:328:15:328:15 | t1 | test.swift:329:15:329:15 | t1 |
261-
| test.swift:329:15:329:15 | [post] t1 | test.swift:330:15:330:15 | t1 |
262-
| test.swift:329:15:329:15 | t1 | test.swift:330:15:330:15 | t1 |
263-
| test.swift:330:15:330:15 | [post] t1 | test.swift:331:15:331:15 | t1 |
264-
| test.swift:330:15:330:15 | t1 | test.swift:331:15:331:15 | t1 |
273+
| test.swift:330:15:330:15 | t2 | test.swift:331:15:331:15 | t2 |
274+
| test.swift:331:15:331:15 | [post] t2 | test.swift:332:15:332:15 | t2 |
275+
| test.swift:331:15:331:15 | t2 | test.swift:332:15:332:15 | t2 |
276+
| test.swift:332:15:332:15 | [post] t2 | test.swift:333:15:333:15 | t2 |
265277
| test.swift:332:15:332:15 | t2 | test.swift:333:15:333:15 | t2 |
266-
| test.swift:333:15:333:15 | [post] t2 | test.swift:334:15:334:15 | t2 |
267-
| test.swift:333:15:333:15 | t2 | test.swift:334:15:334:15 | t2 |
268-
| test.swift:334:15:334:15 | [post] t2 | test.swift:335:15:335:15 | t2 |
269-
| test.swift:334:15:334:15 | t2 | test.swift:335:15:335:15 | t2 |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ func test_optionals(y: Int?) {
271271
sink(opt: x?.signum()) // $ flow=259
272272
sink(opt: y?.signum())
273273

274-
sink(arg: x ?? 0) // $ MISSING: flow=259
275-
sink(arg: x ?? source()) // $ MISSING: flow=259, 276
274+
sink(arg: x ?? 0) // $ flow=259
275+
sink(arg: x ?? source()) // $ flow=259 MISSING: flow=276
276276
sink(arg: y ?? 0)
277277
sink(arg: y ?? source()) // $ MISSING: flow=278
278278

0 commit comments

Comments
 (0)