Skip to content

Commit d3c68c7

Browse files
committed
Swift: Add Dictionary models
1 parent 70c2ef5 commit d3c68c7

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

swift/ql/lib/codeql/swift/frameworks/StandardLibrary/Dictionary.qll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,17 @@ private import codeql.swift.dataflow.ExternalFlow
77
class CanonicalDictionaryType extends BoundGenericType {
88
CanonicalDictionaryType() { this.getName().matches("Dictionary<%") }
99
}
10+
11+
/**
12+
* A model for `Dictionary` and related class members that permit data flow.
13+
*/
14+
private class DictionarySummaries extends SummaryModelCsv {
15+
override predicate row(string row) {
16+
row =
17+
[
18+
";Dictionary;true;updateValue(_:forKey:);;;Argument[0];Argument[-1].CollectionElement.TupleElement[1];value",
19+
";Dictionary;true;updateValue(_:forKey:);;;Argument[1];Argument[-1].CollectionElement.TupleElement[0];value",
20+
";Dictionary;true;updateValue(_:forKey:);;;Argument[-1].CollectionElement.TupleElement[1];ReturnValue.OptionalSome;value"
21+
]
22+
}
23+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,13 @@ edges
415415
| test.swift:789:17:789:28 | [...] [Collection element, Tuple element at index 1] | test.swift:793:15:793:15 | dict4 [Collection element, Tuple element at index 1] |
416416
| test.swift:789:18:789:27 | (...) [Tuple element at index 1] | test.swift:789:17:789:28 | [...] [Collection element, Tuple element at index 1] |
417417
| test.swift:789:20:789:27 | call to source() | test.swift:789:18:789:27 | (...) [Tuple element at index 1] |
418+
| test.swift:790:15:790:15 | [post] dict4 [Collection element, Tuple element at index 0] | test.swift:792:15:792:15 | dict4 [Collection element, Tuple element at index 0] |
419+
| test.swift:790:44:790:51 | call to source() | test.swift:790:15:790:15 | [post] dict4 [Collection element, Tuple element at index 0] |
420+
| test.swift:791:15:791:15 | [post] dict4 [Collection element, Tuple element at index 1] | test.swift:793:15:793:15 | dict4 [Collection element, Tuple element at index 1] |
421+
| test.swift:791:33:791:40 | call to source() | test.swift:791:15:791:15 | [post] dict4 [Collection element, Tuple element at index 1] |
422+
| test.swift:792:15:792:15 | dict4 [Collection element, Tuple element at index 0] | test.swift:792:15:792:35 | call to randomElement() [some:0, Tuple element at index 0] |
423+
| test.swift:792:15:792:35 | call to randomElement() [some:0, Tuple element at index 0] | test.swift:792:15:792:36 | ...! [Tuple element at index 0] |
424+
| test.swift:792:15:792:36 | ...! [Tuple element at index 0] | test.swift:792:15:792:38 | .0 |
418425
| test.swift:793:15:793:15 | dict4 [Collection element, Tuple element at index 1] | test.swift:793:15:793:35 | call to randomElement() [some:0, Tuple element at index 1] |
419426
| test.swift:793:15:793:35 | call to randomElement() [some:0, Tuple element at index 1] | test.swift:793:15:793:36 | ...! [Tuple element at index 1] |
420427
| test.swift:793:15:793:36 | ...! [Tuple element at index 1] | test.swift:793:15:793:38 | .1 |
@@ -876,6 +883,14 @@ nodes
876883
| test.swift:789:17:789:28 | [...] [Collection element, Tuple element at index 1] | semmle.label | [...] [Collection element, Tuple element at index 1] |
877884
| test.swift:789:18:789:27 | (...) [Tuple element at index 1] | semmle.label | (...) [Tuple element at index 1] |
878885
| test.swift:789:20:789:27 | call to source() | semmle.label | call to source() |
886+
| test.swift:790:15:790:15 | [post] dict4 [Collection element, Tuple element at index 0] | semmle.label | [post] dict4 [Collection element, Tuple element at index 0] |
887+
| test.swift:790:44:790:51 | call to source() | semmle.label | call to source() |
888+
| test.swift:791:15:791:15 | [post] dict4 [Collection element, Tuple element at index 1] | semmle.label | [post] dict4 [Collection element, Tuple element at index 1] |
889+
| test.swift:791:33:791:40 | call to source() | semmle.label | call to source() |
890+
| test.swift:792:15:792:15 | dict4 [Collection element, Tuple element at index 0] | semmle.label | dict4 [Collection element, Tuple element at index 0] |
891+
| test.swift:792:15:792:35 | call to randomElement() [some:0, Tuple element at index 0] | semmle.label | call to randomElement() [some:0, Tuple element at index 0] |
892+
| test.swift:792:15:792:36 | ...! [Tuple element at index 0] | semmle.label | ...! [Tuple element at index 0] |
893+
| test.swift:792:15:792:38 | .0 | semmle.label | .0 |
879894
| test.swift:793:15:793:15 | dict4 [Collection element, Tuple element at index 1] | semmle.label | dict4 [Collection element, Tuple element at index 1] |
880895
| test.swift:793:15:793:35 | call to randomElement() [some:0, Tuple element at index 1] | semmle.label | call to randomElement() [some:0, Tuple element at index 1] |
881896
| test.swift:793:15:793:36 | ...! [Tuple element at index 1] | semmle.label | ...! [Tuple element at index 1] |
@@ -1029,4 +1044,6 @@ subpaths
10291044
| test.swift:757:15:757:19 | .v3 | test.swift:747:14:747:21 | call to source() | test.swift:757:15:757:19 | .v3 | result |
10301045
| test.swift:769:15:769:22 | ...[...] | test.swift:767:16:767:23 | call to source() | test.swift:769:15:769:22 | ...[...] | result |
10311046
| test.swift:780:15:780:22 | ...[...] | test.swift:779:21:779:28 | call to source() | test.swift:780:15:780:22 | ...[...] | result |
1047+
| test.swift:792:15:792:38 | .0 | test.swift:790:44:790:51 | call to source() | test.swift:792:15:792:38 | .0 | result |
10321048
| test.swift:793:15:793:38 | .1 | test.swift:789:20:789:27 | call to source() | test.swift:793:15:793:38 | .1 | result |
1049+
| test.swift:793:15:793:38 | .1 | test.swift:791:33:791:40 | call to source() | test.swift:793:15:793:38 | .1 | result |

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -789,8 +789,8 @@ func testDictionary() {
789789
var dict4 = [1:source()]
790790
sink(arg: dict4.updateValue(1, forKey: source())!)
791791
sink(arg: dict4.updateValue(source(), forKey: 2)!)
792-
sink(arg: dict4.randomElement()!.0) // $ MISSING: flow=791
793-
sink(arg: dict4.randomElement()!.1) // $ flow=789 MISSING: flow=790
794-
sink(arg: dict4.keys.randomElement()) // $ MISSING: flow=791
795-
sink(arg: dict4.values.randomElement()) // $ MISSING: flow=789 flow=790
792+
sink(arg: dict4.randomElement()!.0) // $ flow=790
793+
sink(arg: dict4.randomElement()!.1) // $ flow=789 flow=791
794+
sink(arg: dict4.keys.randomElement()) // $ MISSING: flow=790
795+
sink(arg: dict4.values.randomElement()) // $ MISSING: flow=789 flow=791
796796
}

0 commit comments

Comments
 (0)