Skip to content

Commit 872da71

Browse files
asgerfCopilot
andcommitted
Unified: Simplify MaD matching of setter method calls
Drop the ':set' suffix, as setter methods are already distinguished by their '#newValue' argument label, and match setter calls in the same pipeline as ordinary method calls. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 6d89a1e commit 872da71

3 files changed

Lines changed: 19 additions & 30 deletions

File tree

‎unified/ql/lib/codeql/unified/internal/mad/LegacyMaD.qll‎

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,20 @@ private string getArgLabelsFromCall(CallExpr call) {
7171
)
7272
}
7373

74-
pragma[nomagic]
75-
private predicate methodCallSelector(CallExpr call, string name, string argLabels) {
76-
name = getSimpleNameFromExpr(call.getCallee()) and
77-
argLabels = getArgLabelsFromCall(call)
78-
}
79-
8074
/**
81-
* Holds if `call` invokes a setter that models refer to as `name`. By convention, the setter
82-
* method invoked by a call to `foo` is named `foo:set` in models, such as `subscript:set` for
83-
* `x[i] = y`.
75+
* Holds if `call` is a method call with the given `name` and `argLabels`.
8476
*
85-
* The new value is passed as an extra named argument `#newValue` after the explicit ones, so
86-
* models refer to it as `Argument[#newValue:]`, e.g. `subscript:set(_:#newValue:)`.
77+
* A call to a setter method, such as `x[i]` in `x[i] = y`, passes the new value as an extra
78+
* argument named `#newValue`, so models refer to it as e.g. `subscript(_:#newValue:)`.
8779
*/
8880
pragma[nomagic]
89-
private predicate setterCallSelector(CallExpr call, string name, string argLabels) {
90-
name = call.getCallee().(MemberAccessExpr).getMemberName() + ":set" and
91-
argLabels = getArgLabelsFromCall(call) + "#newValue:"
81+
private predicate methodCallSelector(DataFlowCall call, string name, string argLabels) {
82+
exists(CallExpr e | e = call.getCallExpr() |
83+
name = getSimpleNameFromExpr(e.getCallee()) and
84+
if e = call.asSetterCall()
85+
then argLabels = getArgLabelsFromCall(e) + "#newValue:"
86+
else argLabels = getArgLabelsFromCall(e)
87+
)
9288
}
9389

9490
pragma[nomagic]
@@ -126,13 +122,10 @@ private string getArgLabelsFromCallable(Callable callable) {
126122
}
127123

128124
pragma[nomagic]
129-
private predicate callableSelector(Callable callable, string type, string name, string argLabels) {
130-
(
131-
name = callable.(FunctionDeclaration).getName() and
132-
not callable instanceof SetterMethodDeclaration
133-
or
134-
name = callable.(SetterMethodDeclaration).getName() + ":set"
135-
) and
125+
private predicate callableSelector(
126+
FunctionDeclaration callable, string type, string name, string argLabels
127+
) {
128+
name = callable.getName() and
136129
argLabels = getArgLabelsFromCallable(callable) and
137130
exists(ClassLikeDeclaration cls |
138131
callable = cls.getAMemberIncludingExtensions() and
@@ -230,10 +223,7 @@ class Selector extends TSelector {
230223
predicate matchesCall(DataFlowCall call) {
231224
exists(string name, string argLabels |
232225
this.matchesMethodCallSelector(name, argLabels) and
233-
methodCallSelector(call.asExplicitCall(), name, argLabels)
234-
or
235-
this.matchesMethodCallSelector(name, argLabels) and
236-
setterCallSelector(call.asSetterCall(), name, argLabels)
226+
methodCallSelector(call, name, argLabels)
237227
or
238228
this.matchesConstructorCallSelector(name, argLabels) and
239229
constructorCallSelector(call.asExplicitCall(), name, argLabels)
@@ -370,8 +360,7 @@ private DataFlow::Node getSinkFromCall(DataFlowCall call, AccessPathToken token)
370360
// The new value passed to a setter method, as the named argument `#newValue`
371361
token.getName() = "Argument" and
372362
token.getAnArgument() = "#newValue:" and
373-
result.(BuilderNode).isSetterValueArgument(call) and
374-
call.asSetterCall() instanceof CallExpr
363+
result.(BuilderNode).isSetterValueArgument(call)
375364
or
376365
token.getName() = "Argument" and
377366
token.getAnArgument() = "self" and

‎unified/ql/test/library-tests/dataflow/test.ext.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
["", "TestType", true, "setField(_:)", "", "", "Argument[0]", "Argument[self].Member[field]", "value", "manual"],
2525
["", "TestType", true, "readField(_:)", "", "", "Argument[0].Member[field]", "ReturnValue", "value", "manual"],
2626
["", "TestType", true, "subscript(testKey:)", "", "", "Argument[testKey:]", "ReturnValue", "value", "manual"],
27-
["", "TestType", true, "subscript:set(testKey:#newValue:)", "", "", "Argument[#newValue:]", "Argument[self].Member[field]", "value", "manual"]
27+
["", "TestType", true, "subscript(testKey:#newValue:)", "", "", "Argument[#newValue:]", "Argument[self].Member[field]", "value", "manual"]
2828
]
2929
}
3030
]

‎unified/ql/test/library-tests/mad/test.ext.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"data": [
99
// namespace, type, subtypes, name, signature, ext, output, kind, provenance
1010
["", "ShorthandTest", true, "sourceParams(_:)", "", "", "Parameter[self,0]", "shorthand-source", "manual"],
11-
["", "SubscriptTest", true, "subscript:set(_:#newValue:)", "", "", "Parameter[#newValue:]", "subscript-setter-source", "manual"]
11+
["", "SubscriptTest", true, "subscript(_:#newValue:)", "", "", "Parameter[#newValue:]", "subscript-setter-source", "manual"]
1212
]
1313
},
1414
{
@@ -20,7 +20,7 @@
2020
// namespace, type, subtypes, name, signature, ext, input, kind, provenance
2121
["", "ShorthandTest", true, "sinkArgs(_:)", "", "", "Argument[self,0]", "shorthand-sink", "manual"],
2222
["", "SubscriptTest", true, "subscript(_:)", "", "", "Argument[0]", "subscript-getter-sink", "manual"],
23-
["", "SubscriptTest", true, "subscript:set(_:#newValue:)", "", "", "Argument[#newValue:]", "subscript-setter-sink", "manual"]
23+
["", "SubscriptTest", true, "subscript(_:#newValue:)", "", "", "Argument[#newValue:]", "subscript-setter-sink", "manual"]
2424
]
2525
}
2626
]

0 commit comments

Comments
 (0)