Skip to content

Commit 79368c1

Browse files
committed
Swift: fixes around DictionaryContent
1 parent a9f5471 commit 79368c1

File tree

4 files changed

+59
-16
lines changed

4 files changed

+59
-16
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -747,24 +747,26 @@ predicate storeStep(Node node1, ContentSet c, Node node2) {
747747
c.isSingleton(any(Content::ArrayContent ac))
748748
)
749749
or
750-
// read of a dictionary value via subscript operator, with intermediate step
750+
// assignment to a dictionary value via subscript operator, with intermediate step
751+
// `dict[key] = value`
751752
exists(AssignExpr assign, SubscriptExpr subscript |
752753
subscript = assign.getDest() and
753754
(
754755
subscript.getArgument(0).getExpr() = node1.asExpr() and
755756
node2.(DictionarySubscriptNode).getExpr() = subscript and
756-
c.isSingleton(any(Content::TupleContent tc | tc.getIndex() = 1))
757+
c.isSingleton(any(Content::TupleContent tc | tc.getIndex() = 0))
757758
or
758759
assign.getSource() = node1.asExpr() and
759760
node2.(DictionarySubscriptNode).getExpr() = subscript and
760761
c.isSingleton(any(Content::TupleContent tc | tc.getIndex() = 1))
761762
or
762-
node1.(DictionarySubscriptNode) = node1 and
763-
node2.asExpr() = subscript and
763+
node1.(DictionarySubscriptNode).getExpr() = subscript and
764+
node2.(PostUpdateNode).getPreUpdateNode().asExpr() = subscript.getBase() and
764765
c.isSingleton(any(Content::CollectionContent cc))
765766
)
766767
)
767768
or
769+
// creation of a dictionary `[key: value, ...]`
768770
exists(DictionaryExpr dict |
769771
node1.asExpr() = dict.getAnElement() and
770772
node2.asExpr() = dict and
@@ -872,7 +874,11 @@ predicate readStep(Node node1, ContentSet c, Node node2) {
872874
* in `x.f = newValue`.
873875
*/
874876
predicate clearsContent(Node n, ContentSet c) {
875-
n = any(PostUpdateNode pun | storeStep(_, c, pun)).getPreUpdateNode()
877+
n = any(PostUpdateNode pun | storeStep(_, c, pun)).getPreUpdateNode() and
878+
(
879+
c.isSingleton(any(Content::FieldContent fc)) or
880+
c.isSingleton(any(Content::TupleContent tc))
881+
)
876882
}
877883

878884
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ private import codeql.swift.dataflow.ExternalFlow
99
* An instance of the `Array` type.
1010
*/
1111
class ArrayType extends Type {
12-
ArrayType() { this.getName().matches("Array<%") or this.getName().matches("[%]") }
12+
ArrayType() { this.getCanonicalType().getName().matches("Array<%") }
1313
}
1414

1515
/**

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

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,25 +404,42 @@ edges
404404
| test.swift:756:15:756:19 | .v2 [some:0] | test.swift:756:15:756:21 | ...! |
405405
| test.swift:757:15:757:15 | mo1 [v3] | test.swift:732:9:732:9 | self [v3] |
406406
| test.swift:757:15:757:15 | mo1 [v3] | test.swift:757:15:757:19 | .v3 |
407-
| test.swift:767:5:767:5 | [post] dict1 [Array element] | test.swift:769:15:769:15 | dict1 [Array element] |
408-
| test.swift:767:16:767:23 | call to source() | test.swift:767:5:767:5 | [post] dict1 [Array element] |
409-
| test.swift:769:15:769:15 | dict1 [Array element] | test.swift:769:15:769:22 | ...[...] |
407+
| test.swift:767:5:767:5 | [post] dict1 [Collection element, Tuple element at index 1] | test.swift:769:15:769:15 | dict1 [Collection element, Tuple element at index 1] |
408+
| test.swift:767:5:767:12 | DictionarySubscriptNode [Tuple element at index 1] | test.swift:767:5:767:5 | [post] dict1 [Collection element, Tuple element at index 1] |
409+
| test.swift:767:16:767:23 | call to source() | test.swift:767:5:767:12 | DictionarySubscriptNode [Tuple element at index 1] |
410+
| test.swift:769:15:769:15 | dict1 [Collection element, Tuple element at index 1] | test.swift:769:15:769:22 | DictionarySubscriptNode [Tuple element at index 1] |
411+
| test.swift:769:15:769:22 | DictionarySubscriptNode [Tuple element at index 1] | test.swift:769:15:769:22 | ...[...] |
410412
| test.swift:779:17:779:29 | [...] [Collection element, Tuple element at index 1] | test.swift:780:15:780:15 | dict3 [Collection element, Tuple element at index 1] |
411413
| test.swift:779:17:779:29 | [...] [Collection element, Tuple element at index 1] | test.swift:782:5:782:5 | dict3 [Collection element, Tuple element at index 1] |
414+
| test.swift:779:17:779:29 | [...] [Collection element, Tuple element at index 1] | test.swift:785:15:785:15 | dict3 [Collection element, Tuple element at index 1] |
412415
| test.swift:779:18:779:28 | (...) [Tuple element at index 1] | test.swift:779:17:779:29 | [...] [Collection element, Tuple element at index 1] |
413416
| test.swift:779:21:779:28 | call to source() | test.swift:779:18:779:28 | (...) [Tuple element at index 1] |
414417
| test.swift:780:15:780:15 | dict3 [Collection element, Tuple element at index 1] | test.swift:780:15:780:22 | DictionarySubscriptNode [Tuple element at index 1] |
415418
| test.swift:780:15:780:22 | DictionarySubscriptNode [Tuple element at index 1] | test.swift:780:15:780:22 | ...[...] |
416-
| test.swift:782:5:782:5 | dict3 [Collection element, Tuple element at index 1] | test.swift:785:15:785:15 | dict3 [Collection element, Tuple element at index 1] |
419+
| test.swift:782:5:782:5 | [post] dict3 [Collection element, Tuple element at index 0] | test.swift:784:15:784:15 | dict3 [Collection element, Tuple element at index 0] |
420+
| test.swift:782:5:782:5 | [post] dict3 [Collection element, Tuple element at index 1] | test.swift:785:15:785:15 | dict3 [Collection element, Tuple element at index 1] |
421+
| test.swift:782:5:782:5 | dict3 [Collection element, Tuple element at index 1] | test.swift:782:5:782:19 | DictionarySubscriptNode [Tuple element at index 1] |
422+
| test.swift:782:5:782:19 | DictionarySubscriptNode [Tuple element at index 0] | test.swift:782:5:782:5 | [post] dict3 [Collection element, Tuple element at index 0] |
423+
| test.swift:782:5:782:19 | DictionarySubscriptNode [Tuple element at index 1] | test.swift:782:5:782:5 | [post] dict3 [Collection element, Tuple element at index 1] |
424+
| test.swift:782:11:782:18 | call to source() | test.swift:782:5:782:19 | DictionarySubscriptNode [Tuple element at index 0] |
425+
| test.swift:784:15:784:15 | dict3 [Collection element, Tuple element at index 0] | test.swift:784:15:784:35 | call to randomElement() [some:0, Tuple element at index 0] |
426+
| test.swift:784:15:784:35 | call to randomElement() [some:0, Tuple element at index 0] | test.swift:784:15:784:36 | ...! [Tuple element at index 0] |
427+
| test.swift:784:15:784:36 | ...! [Tuple element at index 0] | test.swift:784:15:784:38 | .0 |
417428
| test.swift:785:15:785:15 | dict3 [Collection element, Tuple element at index 1] | test.swift:785:15:785:35 | call to randomElement() [some:0, Tuple element at index 1] |
418429
| test.swift:785:15:785:35 | call to randomElement() [some:0, Tuple element at index 1] | test.swift:785:15:785:36 | ...! [Tuple element at index 1] |
419430
| test.swift:785:15:785:36 | ...! [Tuple element at index 1] | test.swift:785:15:785:38 | .1 |
431+
| test.swift:792:17:792:28 | [...] [Collection element, Tuple element at index 1] | test.swift:793:15:793:15 | dict4 [Collection element, Tuple element at index 1] |
432+
| test.swift:792:17:792:28 | [...] [Collection element, Tuple element at index 1] | test.swift:794:15:794:15 | dict4 [Collection element, Tuple element at index 1] |
420433
| test.swift:792:17:792:28 | [...] [Collection element, Tuple element at index 1] | test.swift:796:15:796:15 | dict4 [Collection element, Tuple element at index 1] |
421434
| test.swift:792:18:792:27 | (...) [Tuple element at index 1] | test.swift:792:17:792:28 | [...] [Collection element, Tuple element at index 1] |
422435
| test.swift:792:20:792:27 | call to source() | test.swift:792:18:792:27 | (...) [Tuple element at index 1] |
423436
| test.swift:793:15:793:15 | [post] dict4 [Collection element, Tuple element at index 0] | test.swift:795:15:795:15 | dict4 [Collection element, Tuple element at index 0] |
437+
| test.swift:793:15:793:15 | dict4 [Collection element, Tuple element at index 1] | test.swift:793:15:793:52 | call to updateValue(_:forKey:) [some:0] |
438+
| test.swift:793:15:793:52 | call to updateValue(_:forKey:) [some:0] | test.swift:793:15:793:53 | ...! |
424439
| test.swift:793:44:793:51 | call to source() | test.swift:793:15:793:15 | [post] dict4 [Collection element, Tuple element at index 0] |
425440
| test.swift:794:15:794:15 | [post] dict4 [Collection element, Tuple element at index 1] | test.swift:796:15:796:15 | dict4 [Collection element, Tuple element at index 1] |
441+
| test.swift:794:15:794:15 | dict4 [Collection element, Tuple element at index 1] | test.swift:794:15:794:52 | call to updateValue(_:forKey:) [some:0] |
442+
| test.swift:794:15:794:52 | call to updateValue(_:forKey:) [some:0] | test.swift:794:15:794:53 | ...! |
426443
| test.swift:794:33:794:40 | call to source() | test.swift:794:15:794:15 | [post] dict4 [Collection element, Tuple element at index 1] |
427444
| test.swift:795:15:795:15 | dict4 [Collection element, Tuple element at index 0] | test.swift:795:15:795:35 | call to randomElement() [some:0, Tuple element at index 0] |
428445
| test.swift:795:15:795:35 | call to randomElement() [some:0, Tuple element at index 0] | test.swift:795:15:795:36 | ...! [Tuple element at index 0] |
@@ -875,17 +892,28 @@ nodes
875892
| test.swift:756:15:756:21 | ...! | semmle.label | ...! |
876893
| test.swift:757:15:757:15 | mo1 [v3] | semmle.label | mo1 [v3] |
877894
| test.swift:757:15:757:19 | .v3 | semmle.label | .v3 |
878-
| test.swift:767:5:767:5 | [post] dict1 [Array element] | semmle.label | [post] dict1 [Array element] |
895+
| test.swift:767:5:767:5 | [post] dict1 [Collection element, Tuple element at index 1] | semmle.label | [post] dict1 [Collection element, Tuple element at index 1] |
896+
| test.swift:767:5:767:12 | DictionarySubscriptNode [Tuple element at index 1] | semmle.label | DictionarySubscriptNode [Tuple element at index 1] |
879897
| test.swift:767:16:767:23 | call to source() | semmle.label | call to source() |
880-
| test.swift:769:15:769:15 | dict1 [Array element] | semmle.label | dict1 [Array element] |
898+
| test.swift:769:15:769:15 | dict1 [Collection element, Tuple element at index 1] | semmle.label | dict1 [Collection element, Tuple element at index 1] |
881899
| test.swift:769:15:769:22 | ...[...] | semmle.label | ...[...] |
900+
| test.swift:769:15:769:22 | DictionarySubscriptNode [Tuple element at index 1] | semmle.label | DictionarySubscriptNode [Tuple element at index 1] |
882901
| test.swift:779:17:779:29 | [...] [Collection element, Tuple element at index 1] | semmle.label | [...] [Collection element, Tuple element at index 1] |
883902
| test.swift:779:18:779:28 | (...) [Tuple element at index 1] | semmle.label | (...) [Tuple element at index 1] |
884903
| test.swift:779:21:779:28 | call to source() | semmle.label | call to source() |
885904
| test.swift:780:15:780:15 | dict3 [Collection element, Tuple element at index 1] | semmle.label | dict3 [Collection element, Tuple element at index 1] |
886905
| test.swift:780:15:780:22 | ...[...] | semmle.label | ...[...] |
887906
| test.swift:780:15:780:22 | DictionarySubscriptNode [Tuple element at index 1] | semmle.label | DictionarySubscriptNode [Tuple element at index 1] |
907+
| test.swift:782:5:782:5 | [post] dict3 [Collection element, Tuple element at index 0] | semmle.label | [post] dict3 [Collection element, Tuple element at index 0] |
908+
| test.swift:782:5:782:5 | [post] dict3 [Collection element, Tuple element at index 1] | semmle.label | [post] dict3 [Collection element, Tuple element at index 1] |
888909
| test.swift:782:5:782:5 | dict3 [Collection element, Tuple element at index 1] | semmle.label | dict3 [Collection element, Tuple element at index 1] |
910+
| test.swift:782:5:782:19 | DictionarySubscriptNode [Tuple element at index 0] | semmle.label | DictionarySubscriptNode [Tuple element at index 0] |
911+
| test.swift:782:5:782:19 | DictionarySubscriptNode [Tuple element at index 1] | semmle.label | DictionarySubscriptNode [Tuple element at index 1] |
912+
| test.swift:782:11:782:18 | call to source() | semmle.label | call to source() |
913+
| test.swift:784:15:784:15 | dict3 [Collection element, Tuple element at index 0] | semmle.label | dict3 [Collection element, Tuple element at index 0] |
914+
| test.swift:784:15:784:35 | call to randomElement() [some:0, Tuple element at index 0] | semmle.label | call to randomElement() [some:0, Tuple element at index 0] |
915+
| test.swift:784:15:784:36 | ...! [Tuple element at index 0] | semmle.label | ...! [Tuple element at index 0] |
916+
| test.swift:784:15:784:38 | .0 | semmle.label | .0 |
889917
| test.swift:785:15:785:15 | dict3 [Collection element, Tuple element at index 1] | semmle.label | dict3 [Collection element, Tuple element at index 1] |
890918
| test.swift:785:15:785:35 | call to randomElement() [some:0, Tuple element at index 1] | semmle.label | call to randomElement() [some:0, Tuple element at index 1] |
891919
| test.swift:785:15:785:36 | ...! [Tuple element at index 1] | semmle.label | ...! [Tuple element at index 1] |
@@ -894,8 +922,14 @@ nodes
894922
| test.swift:792:18:792:27 | (...) [Tuple element at index 1] | semmle.label | (...) [Tuple element at index 1] |
895923
| test.swift:792:20:792:27 | call to source() | semmle.label | call to source() |
896924
| test.swift:793:15:793:15 | [post] dict4 [Collection element, Tuple element at index 0] | semmle.label | [post] dict4 [Collection element, Tuple element at index 0] |
925+
| test.swift:793:15:793:15 | dict4 [Collection element, Tuple element at index 1] | semmle.label | dict4 [Collection element, Tuple element at index 1] |
926+
| test.swift:793:15:793:52 | call to updateValue(_:forKey:) [some:0] | semmle.label | call to updateValue(_:forKey:) [some:0] |
927+
| test.swift:793:15:793:53 | ...! | semmle.label | ...! |
897928
| test.swift:793:44:793:51 | call to source() | semmle.label | call to source() |
898929
| test.swift:794:15:794:15 | [post] dict4 [Collection element, Tuple element at index 1] | semmle.label | [post] dict4 [Collection element, Tuple element at index 1] |
930+
| test.swift:794:15:794:15 | dict4 [Collection element, Tuple element at index 1] | semmle.label | dict4 [Collection element, Tuple element at index 1] |
931+
| test.swift:794:15:794:52 | call to updateValue(_:forKey:) [some:0] | semmle.label | call to updateValue(_:forKey:) [some:0] |
932+
| test.swift:794:15:794:53 | ...! | semmle.label | ...! |
899933
| test.swift:794:33:794:40 | call to source() | semmle.label | call to source() |
900934
| test.swift:795:15:795:15 | dict4 [Collection element, Tuple element at index 0] | semmle.label | dict4 [Collection element, Tuple element at index 0] |
901935
| test.swift:795:15:795:35 | call to randomElement() [some:0, Tuple element at index 0] | semmle.label | call to randomElement() [some:0, Tuple element at index 0] |
@@ -1054,7 +1088,10 @@ subpaths
10541088
| test.swift:757:15:757:19 | .v3 | test.swift:747:14:747:21 | call to source() | test.swift:757:15:757:19 | .v3 | result |
10551089
| test.swift:769:15:769:22 | ...[...] | test.swift:767:16:767:23 | call to source() | test.swift:769:15:769:22 | ...[...] | result |
10561090
| test.swift:780:15:780:22 | ...[...] | test.swift:779:21:779:28 | call to source() | test.swift:780:15:780:22 | ...[...] | result |
1091+
| test.swift:784:15:784:38 | .0 | test.swift:782:11:782:18 | call to source() | test.swift:784:15:784:38 | .0 | result |
10571092
| test.swift:785:15:785:38 | .1 | test.swift:779:21:779:28 | call to source() | test.swift:785:15:785:38 | .1 | result |
1093+
| test.swift:793:15:793:53 | ...! | test.swift:792:20:792:27 | call to source() | test.swift:793:15:793:53 | ...! | result |
1094+
| test.swift:794:15:794:53 | ...! | test.swift:792:20:792:27 | call to source() | test.swift:794:15:794:53 | ...! | result |
10581095
| test.swift:795:15:795:38 | .0 | test.swift:793:44:793:51 | call to source() | test.swift:795:15:795:38 | .0 | result |
10591096
| test.swift:796:15:796:38 | .1 | test.swift:792:20:792:27 | call to source() | test.swift:796:15:796:38 | .1 | result |
10601097
| test.swift:796:15:796:38 | .1 | test.swift:794:33:794:40 | call to source() | test.swift:796:15:796: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
@@ -781,17 +781,17 @@ func testDictionary() {
781781

782782
dict3[source()] = 2
783783

784-
sink(arg: dict3.randomElement()!.0) // $ MISSING: flow=779
785-
sink(arg: dict3.randomElement()!.1) // $ SPURIOUS: flow=779 MISSING: flow=782
784+
sink(arg: dict3.randomElement()!.0) // $ flow=782
785+
sink(arg: dict3.randomElement()!.1) // $ flow=779
786786

787787
for (key, value) in dict3 {
788788
sink(arg: key) // $ MISSING: flow=782
789789
sink(arg: value) // $ MISSING: flow=779
790790
}
791791

792792
var dict4 = [1:source()]
793-
sink(arg: dict4.updateValue(1, forKey: source())!)
794-
sink(arg: dict4.updateValue(source(), forKey: 2)!)
793+
sink(arg: dict4.updateValue(1, forKey: source())!) // $ flow=792
794+
sink(arg: dict4.updateValue(source(), forKey: 2)!) // $ SPURIOUS: flow=792
795795
sink(arg: dict4.randomElement()!.0) // $ flow=793
796796
sink(arg: dict4.randomElement()!.1) // $ flow=792 flow=794
797797
sink(arg: dict4.keys.randomElement()) // $ MISSING: flow=793

0 commit comments

Comments
 (0)