Skip to content

Commit 397b834

Browse files
committed
Data flow: Fix bug for sugared call arguments
1 parent 0de27bb commit 397b834

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

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

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,20 @@ module LocalFlow {
9797
}
9898
}
9999

100-
/** An argument of a call (including qualifier arguments). */
101-
private class Argument extends Expr {
102-
private Call call;
100+
/** An argument of a call (including qualifier arguments, excluding block arguments). */
101+
private class Argument extends CfgNodes::ExprCfgNode {
102+
private CfgNodes::ExprNodes::CallCfgNode call;
103103
private int arg;
104104

105-
Argument() { this = call.getArgument(arg) }
105+
Argument() {
106+
this = call.getArgument(arg) and
107+
not this.getExpr() instanceof BlockArgument
108+
or
109+
this = call.getReceiver() and arg = -1
110+
}
106111

107112
/** Holds if this expression is the `i`th argument of `c`. */
108-
predicate isArgumentOf(Expr c, int i) { c = call and i = arg }
113+
predicate isArgumentOf(CfgNodes::ExprNodes::CallCfgNode c, int i) { c = call and i = arg }
109114
}
110115

111116
/** A collection of cached types and predicates to be evaluated in the same stage. */
@@ -125,14 +130,7 @@ private module Cached {
125130
TNormalParameterNode(Parameter p) { not p instanceof BlockParameter } or
126131
TSelfParameterNode(MethodBase m) or
127132
TBlockParameterNode(MethodBase m) or
128-
TExprPostUpdateNode(CfgNodes::ExprCfgNode n) {
129-
exists(AstNode node | node = n.getNode() |
130-
node instanceof Argument and
131-
not node instanceof BlockArgument
132-
or
133-
n = any(CfgNodes::ExprNodes::CallCfgNode call).getReceiver()
134-
)
135-
} or
133+
TExprPostUpdateNode(CfgNodes::ExprCfgNode n) { n instanceof Argument } or
136134
TSummaryNode(
137135
FlowSummaryImpl::Public::SummarizedCallable c,
138136
FlowSummaryImpl::Private::SummaryNodeState state
@@ -438,24 +436,18 @@ abstract class ArgumentNode extends Node {
438436
private module ArgumentNodes {
439437
/** A data-flow node that represents an explicit call argument. */
440438
class ExplicitArgumentNode extends ArgumentNode {
441-
ExplicitArgumentNode() {
442-
this.asExpr().getExpr() instanceof Argument and
443-
not this.asExpr().getExpr() instanceof BlockArgument
444-
}
439+
Argument arg;
440+
441+
ExplicitArgumentNode() { this.asExpr() = arg }
445442

446443
override predicate sourceArgumentOf(CfgNodes::ExprNodes::CallCfgNode call, int pos) {
447-
this.asExpr() = call.getArgument(pos)
444+
arg.isArgumentOf(call, pos)
448445
}
449446
}
450447

451448
/** A data-flow node that represents the `self` argument of a call. */
452-
class SelfArgumentNode extends ArgumentNode {
453-
SelfArgumentNode() { this.asExpr() = any(CfgNodes::ExprNodes::CallCfgNode call).getReceiver() }
454-
455-
override predicate sourceArgumentOf(CfgNodes::ExprNodes::CallCfgNode call, int pos) {
456-
this.asExpr() = call.getReceiver() and
457-
pos = -1
458-
}
449+
class SelfArgumentNode extends ExplicitArgumentNode {
450+
SelfArgumentNode() { arg.isArgumentOf(_, -1) }
459451
}
460452

461453
/** A data-flow node that represents a block argument. */

ruby/ql/test/library-tests/dataflow/local/Nodes.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ arg
3535
| local_dataflow.rb:51:24:51:24 | 9 | local_dataflow.rb:51:20:51:24 | ... < ... | 0 |
3636
| local_dataflow.rb:55:1:55:14 | self | local_dataflow.rb:55:1:55:14 | call to foo | -1 |
3737
| local_dataflow.rb:55:5:55:13 | Array | local_dataflow.rb:55:5:55:13 | call to [] | -1 |
38+
| local_dataflow.rb:55:5:55:13 | call to [] | local_dataflow.rb:55:1:55:14 | call to foo | 0 |
3839
| local_dataflow.rb:55:6:55:6 | 1 | local_dataflow.rb:55:5:55:13 | call to [] | 0 |
3940
| local_dataflow.rb:55:9:55:9 | 2 | local_dataflow.rb:55:5:55:13 | call to [] | 1 |
4041
| local_dataflow.rb:55:12:55:12 | 3 | local_dataflow.rb:55:5:55:13 | call to [] | 2 |

0 commit comments

Comments
 (0)