Skip to content

Commit 9aee525

Browse files
asgerfCopilot
andcommitted
Unified: Post-update setter receivers at the assignment
For a call that is part of an assignment target, such as 'x[i]' in 'x[i] = y' or 'x[i].f = y', position the post-update of the callee at the assignment rather than right after the call, since the setter runs after the right-hand side is evaluated. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent eeab6e5 commit 9aee525

3 files changed

Lines changed: 73 additions & 4 deletions

File tree

‎unified/ql/lib/codeql/unified/internal/dataflow/DataFlowNode.qll‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,41 @@ private predicate hasIncomingValueAtCfgNode(Expr expr, ControlFlowNode cfgNode)
2727
)
2828
}
2929

30+
/**
31+
* Gets the assignment target that `e` is part of, where `e` is either the target itself or
32+
* is reached from it via member access bases and callees. For example, `x` and `x.subscript`
33+
* in `x[i].f = y`.
34+
*/
35+
private Expr getEnclosingAssignmentTarget(Expr e) {
36+
hasIncomingValue(e, _) and
37+
result = e
38+
or
39+
exists(MemberAccessExpr member |
40+
e = member.getBase() and
41+
result = getEnclosingAssignmentTarget(member)
42+
)
43+
or
44+
exists(CallExpr call |
45+
e = call.getCallee() and
46+
result = getEnclosingAssignmentTarget(call)
47+
)
48+
}
49+
3050
/** Holds if `expr` has a post-update at `cfgNode` resulting from a surrounding call or assignment. */
3151
predicate hasPostUpdateAfterCallOrAssign(Expr expr, ControlFlowNode cfgNode) {
3252
exists(MemberAccessExpr member |
3353
(hasIncomingValueAtCfgNode(member, cfgNode) or hasPostUpdateAfterCallOrAssign(member, cfgNode)) and
3454
expr = member.getBase()
3555
)
3656
or
37-
exists(CallExpr call | cfgNode.isAfter(call) |
38-
expr = call.getAnArgument().getValue()
39-
or
40-
expr = call.getCallee()
57+
exists(CallExpr call | cfgNode.isAfter(call) | expr = call.getAnArgument().getValue())
58+
or
59+
exists(CallExpr call | expr = call.getCallee() |
60+
// A call that is part of an assignment target, such as `x[i]` in `x[i] = y`, invokes a setter
61+
// that mutates the receiver after the right-hand side has been evaluated.
62+
if exists(getEnclosingAssignmentTarget(call))
63+
then hasIncomingValueAtCfgNode(getEnclosingAssignmentTarget(call), cfgNode)
64+
else cfgNode.isAfter(call)
4165
)
4266
or
4367
exists(Expr target |

‎unified/ql/test/library-tests/dataflow/subscripts.swift‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,32 @@ func s12() {
152152
let c = C()
153153
update(&c[0])
154154
}
155+
156+
func s13() {
157+
class C {
158+
var field: String = "safe"
159+
subscript(i: Int) -> String {
160+
get { "safe" }
161+
set { field = source("s13.1") }
162+
}
163+
}
164+
let c = C()
165+
c[0] = sink(c.field) // no flow: the setter runs after the right-hand side is evaluated
166+
sink(c.field) // $ hasValueFlow=s13.1
167+
}
168+
169+
func s14() {
170+
struct S {
171+
var f: String = "safe"
172+
}
173+
class C {
174+
var field: String = "safe"
175+
subscript(i: Int) -> S {
176+
get { S() }
177+
set { field = source("s14.1") }
178+
}
179+
}
180+
let c = C()
181+
c[0].f = sink(c.field) // no flow: the setter runs after the right-hand side is evaluated
182+
sink(c.field) // $ hasValueFlow=s14.1
183+
}

‎unified/ql/test/library-tests/dataflow/test.expected‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,12 @@ edges
500500
| subscripts.swift:151:43:151:57 | source(...) | subscripts.swift:151:33:151:39 | ... .field | provenance | |
501501
| subscripts.swift:153:12:153:16 | [post] & ... [field] | subscripts.swift:153:13:153:16 | [post] ... .subscript(...) [field] | provenance | |
502502
| subscripts.swift:153:13:153:16 | [post] ... .subscript(...) [field] | subscripts.swift:148:24:148:31 | newValue [field] | provenance | |
503+
| subscripts.swift:161:19:161:23 | field | subscripts.swift:166:10:166:10 | c [field] | provenance | |
504+
| subscripts.swift:161:27:161:41 | source(...) | subscripts.swift:161:19:161:23 | field | provenance | |
505+
| subscripts.swift:166:10:166:10 | c [field] | subscripts.swift:166:10:166:16 | ... .field | provenance | |
506+
| subscripts.swift:177:19:177:23 | field | subscripts.swift:182:10:182:10 | c [field] | provenance | |
507+
| subscripts.swift:177:27:177:41 | source(...) | subscripts.swift:177:19:177:23 | field | provenance | |
508+
| subscripts.swift:182:10:182:10 | c [field] | subscripts.swift:182:10:182:16 | ... .field | provenance | |
503509
| test.swift:17:10:17:23 | source(...) | test.swift:17:10:17:32 | ... + ... | provenance | |
504510
| test.swift:18:19:18:32 | source(...) | test.swift:18:10:18:32 | ... + ... | provenance | |
505511
| test.swift:20:13:20:26 | source(...) | test.swift:20:10:20:33 | StringInterpolationExpr | provenance | |
@@ -1281,6 +1287,14 @@ nodes
12811287
| subscripts.swift:151:43:151:57 | source(...) | semmle.label | source(...) |
12821288
| subscripts.swift:153:12:153:16 | [post] & ... [field] | semmle.label | [post] & ... [field] |
12831289
| subscripts.swift:153:13:153:16 | [post] ... .subscript(...) [field] | semmle.label | [post] ... .subscript(...) [field] |
1290+
| subscripts.swift:161:19:161:23 | field | semmle.label | field |
1291+
| subscripts.swift:161:27:161:41 | source(...) | semmle.label | source(...) |
1292+
| subscripts.swift:166:10:166:10 | c [field] | semmle.label | c [field] |
1293+
| subscripts.swift:166:10:166:16 | ... .field | semmle.label | ... .field |
1294+
| subscripts.swift:177:19:177:23 | field | semmle.label | field |
1295+
| subscripts.swift:177:27:177:41 | source(...) | semmle.label | source(...) |
1296+
| subscripts.swift:182:10:182:10 | c [field] | semmle.label | c [field] |
1297+
| subscripts.swift:182:10:182:16 | ... .field | semmle.label | ... .field |
12841298
| test.swift:9:10:9:21 | source(...) | semmle.label | source(...) |
12851299
| test.swift:17:10:17:23 | source(...) | semmle.label | source(...) |
12861300
| test.swift:17:10:17:32 | ... + ... | semmle.label | ... + ... |
@@ -1679,6 +1693,8 @@ testFailures
16791693
| subscripts.swift:135:10:135:36 | ... .subscript(...) | subscripts.swift:135:21:135:35 | source(...) | subscripts.swift:135:10:135:36 | ... .subscript(...) | $@ | subscripts.swift:135:21:135:35 | source(...) | source(...) |
16801694
| subscripts.swift:138:10:138:21 | ... .getField(...) | subscripts.swift:137:26:137:40 | source(...) | subscripts.swift:138:10:138:21 | ... .getField(...) | $@ | subscripts.swift:137:26:137:40 | source(...) | source(...) |
16811695
| subscripts.swift:148:24:148:37 | ... .field | subscripts.swift:151:43:151:57 | source(...) | subscripts.swift:148:24:148:37 | ... .field | $@ | subscripts.swift:151:43:151:57 | source(...) | source(...) |
1696+
| subscripts.swift:166:10:166:16 | ... .field | subscripts.swift:161:27:161:41 | source(...) | subscripts.swift:166:10:166:16 | ... .field | $@ | subscripts.swift:161:27:161:41 | source(...) | source(...) |
1697+
| subscripts.swift:182:10:182:16 | ... .field | subscripts.swift:177:27:177:41 | source(...) | subscripts.swift:182:10:182:16 | ... .field | $@ | subscripts.swift:177:27:177:41 | source(...) | source(...) |
16821698
| test.swift:9:10:9:21 | source(...) | test.swift:9:10:9:21 | source(...) | test.swift:9:10:9:21 | source(...) | $@ | test.swift:9:10:9:21 | source(...) | source(...) |
16831699
| test.swift:17:10:17:32 | ... + ... | test.swift:17:10:17:23 | source(...) | test.swift:17:10:17:32 | ... + ... | $@ | test.swift:17:10:17:23 | source(...) | source(...) |
16841700
| test.swift:18:10:18:32 | ... + ... | test.swift:18:19:18:32 | source(...) | test.swift:18:10:18:32 | ... + ... | $@ | test.swift:18:19:18:32 | source(...) | source(...) |

0 commit comments

Comments
 (0)