Skip to content

Commit effe376

Browse files
committed
Swift: More robust OptionalSomePattern flow.
1 parent aa2e79b commit effe376

File tree

4 files changed

+11
-1
lines changed

4 files changed

+11
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ private module Cached {
182182
// retaining this case increases robustness of flow).
183183
nodeFrom.asExpr() = nodeTo.asExpr().(ForceValueExpr).getSubExpr()
184184
or
185+
// read of an optional .some member via `let x: T = y: T?` pattern matching
186+
// note: similar to `ForceValueExpr` this is ideally a content `readStep` but
187+
// in practice we sometimes have taint on the optional itself.
188+
nodeTo.asPattern() = nodeFrom.asPattern().(OptionalSomePattern).getSubPattern()
189+
or
185190
// flow through `?` and `?.`
186191
nodeFrom.asExpr() = nodeTo.asExpr().(BindOptionalExpr).getSubExpr()
187192
or

swift/ql/test/library-tests/dataflow/taint/core/LocalTaint.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
| conversions.swift:29:12:29:30 | call to String.init(_:) | conversions.swift:29:12:29:32 | .utf8 |
2424
| conversions.swift:29:19:29:29 | call to sourceInt() | conversions.swift:29:12:29:30 | call to String.init(_:) |
2525
| conversions.swift:30:20:30:33 | call to sourceString() | conversions.swift:30:20:30:35 | .utf8 |
26+
| conversions.swift:32:5:32:9 | let ...? | conversions.swift:32:9:32:9 | v |
2627
| conversions.swift:32:9:32:9 | SSA def(v) | conversions.swift:33:13:33:13 | v |
2728
| conversions.swift:32:9:32:9 | v | conversions.swift:32:9:32:9 | SSA def(v) |
2829
| conversions.swift:32:13:32:23 | call to sourceInt() | conversions.swift:32:5:32:9 | let ...? |

swift/ql/test/library-tests/dataflow/taint/core/Taint.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edges
55
| conversions.swift:28:19:28:29 | call to sourceInt() | conversions.swift:28:12:28:30 | call to String.init(_:) |
66
| conversions.swift:29:12:29:30 | call to String.init(_:) | conversions.swift:29:12:29:32 | .utf8 |
77
| conversions.swift:29:19:29:29 | call to sourceInt() | conversions.swift:29:12:29:30 | call to String.init(_:) |
8+
| conversions.swift:32:13:32:23 | call to sourceInt() | conversions.swift:33:13:33:13 | v |
89
| conversions.swift:36:18:36:41 | call to numericCast(_:) | conversions.swift:37:12:37:12 | v2 |
910
| conversions.swift:36:30:36:40 | call to sourceInt() | conversions.swift:36:18:36:41 | call to numericCast(_:) |
1011
| conversions.swift:39:17:39:57 | call to unsafeBitCast(_:to:) | conversions.swift:40:12:40:12 | v4 |
@@ -131,6 +132,8 @@ nodes
131132
| conversions.swift:29:12:29:30 | call to String.init(_:) | semmle.label | call to String.init(_:) |
132133
| conversions.swift:29:12:29:32 | .utf8 | semmle.label | .utf8 |
133134
| conversions.swift:29:19:29:29 | call to sourceInt() | semmle.label | call to sourceInt() |
135+
| conversions.swift:32:13:32:23 | call to sourceInt() | semmle.label | call to sourceInt() |
136+
| conversions.swift:33:13:33:13 | v | semmle.label | v |
134137
| conversions.swift:36:18:36:41 | call to numericCast(_:) | semmle.label | call to numericCast(_:) |
135138
| conversions.swift:36:30:36:40 | call to sourceInt() | semmle.label | call to sourceInt() |
136139
| conversions.swift:37:12:37:12 | v2 | semmle.label | v2 |
@@ -322,6 +325,7 @@ subpaths
322325
| conversions.swift:27:12:27:29 | call to Float.init(_:) | conversions.swift:27:18:27:28 | call to sourceInt() | conversions.swift:27:12:27:29 | call to Float.init(_:) | result |
323326
| conversions.swift:28:12:28:30 | call to String.init(_:) | conversions.swift:28:19:28:29 | call to sourceInt() | conversions.swift:28:12:28:30 | call to String.init(_:) | result |
324327
| conversions.swift:29:12:29:32 | .utf8 | conversions.swift:29:19:29:29 | call to sourceInt() | conversions.swift:29:12:29:32 | .utf8 | result |
328+
| conversions.swift:33:13:33:13 | v | conversions.swift:32:13:32:23 | call to sourceInt() | conversions.swift:33:13:33:13 | v | result |
325329
| conversions.swift:37:12:37:12 | v2 | conversions.swift:36:30:36:40 | call to sourceInt() | conversions.swift:37:12:37:12 | v2 | result |
326330
| conversions.swift:40:12:40:12 | v4 | conversions.swift:39:31:39:41 | call to sourceInt() | conversions.swift:40:12:40:12 | v4 | result |
327331
| conversions.swift:43:12:43:12 | v5 | conversions.swift:42:36:42:46 | call to sourceInt() | conversions.swift:43:12:43:12 | v5 | result |

swift/ql/test/library-tests/dataflow/taint/core/conversions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func testConversions() {
3030
sink(arg: [UInt8](sourceString().utf8)) // $ MISSING: tainted=
3131

3232
if let v = sourceInt() as? UInt {
33-
sink(arg: v) // $ MISSING: tainted=
33+
sink(arg: v) // $ tainted=32
3434
}
3535

3636
let v2: UInt8 = numericCast(sourceInt())

0 commit comments

Comments
 (0)