Skip to content

Commit ae6501d

Browse files
committed
Java: Implement ParameterPosition et al
1 parent 35a6784 commit ae6501d

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

java/ql/lib/semmle/code/java/dataflow/internal/DataFlowDispatch.qll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,22 @@ private module DispatchImpl {
183183
)
184184
)
185185
}
186+
187+
private int parameterPosition() { result in [-1, any(Parameter p).getPosition()] }
188+
189+
/** A parameter position represented by an integer. */
190+
class ParameterPosition extends int {
191+
ParameterPosition() { this = parameterPosition() }
192+
}
193+
194+
/** An argument position represented by an integer. */
195+
class ArgumentPosition extends int {
196+
ArgumentPosition() { this = parameterPosition() }
197+
}
198+
199+
/** Holds if arguments at position `apos` match parameters at position `ppos`. */
200+
pragma[inline]
201+
predicate parameterMatch(ParameterPosition ppos, ArgumentPosition apos) { ppos = apos }
186202
}
187203

188204
import DispatchImpl

java/ql/lib/semmle/code/java/dataflow/internal/DataFlowNodes.qll

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ private class ImplicitExprPostUpdate extends ImplicitPostUpdateNode, TImplicitEx
310310
}
311311

312312
module Private {
313+
private import DataFlowDispatch
314+
313315
/** Gets the callable in which this node occurs. */
314316
DataFlowCallable nodeGetEnclosingCallable(Node n) {
315317
result.asCallable() = n.asExpr().getEnclosingCallable() or
@@ -324,10 +326,15 @@ module Private {
324326
}
325327

326328
/** Holds if `p` is a `ParameterNode` of `c` with position `pos`. */
327-
predicate isParameterNode(ParameterNode p, DataFlowCallable c, int pos) {
329+
predicate isParameterNode(ParameterNode p, DataFlowCallable c, ParameterPosition pos) {
328330
p.isParameterOf(c.asCallable(), pos)
329331
}
330332

333+
/** Holds if `arg` is an `ArgumentNode` of `c` with position `pos`. */
334+
predicate isArgumentNode(ArgumentNode arg, DataFlowCall c, ArgumentPosition pos) {
335+
arg.argumentOf(c, pos)
336+
}
337+
331338
/**
332339
* A data flow node that occurs as the argument of a call and is passed as-is
333340
* to the callable. Arguments that are wrapped in an implicit varargs array

java/ql/lib/semmle/code/java/dataflow/internal/FlowSummaryImplSpecific.qll

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
private import java
6+
private import DataFlowDispatch
67
private import DataFlowPrivate
78
private import DataFlowUtil
89
private import FlowSummaryImpl::Private
@@ -13,9 +14,6 @@ private module FlowSummaries {
1314
private import semmle.code.java.dataflow.FlowSummary as F
1415
}
1516

16-
/** Holds is `i` is a valid parameter position. */
17-
predicate parameterPosition(int i) { i in [-1 .. any(Parameter p).getPosition()] }
18-
1917
/** Gets the parameter position of the instance parameter. */
2018
int instanceParameterPosition() { result = -1 }
2119

@@ -189,3 +187,19 @@ predicate interpretInputSpecific(string c, InterpretNode mid, InterpretNode n) {
189187
n.asNode().asExpr() = fw.getRHS()
190188
)
191189
}
190+
191+
/** Gets the argument position obtained by parsing `X` in `Parameter[X]`. */
192+
bindingset[s]
193+
ArgumentPosition parseParamBody(string s) {
194+
result = s.regexpCapture("([-0-9]+)", 1).toInt()
195+
or
196+
exists(int n1, int n2 |
197+
s.regexpCapture("([-0-9]+)\\.\\.([0-9]+)", 1).toInt() = n1 and
198+
s.regexpCapture("([-0-9]+)\\.\\.([0-9]+)", 2).toInt() = n2 and
199+
result in [n1 .. n2]
200+
)
201+
}
202+
203+
/** Gets the parameter position obtained by parsing `X` in `Argument[X]`. */
204+
bindingset[s]
205+
ParameterPosition parseArgBody(string s) { result = parseParamBody(s) }

0 commit comments

Comments
 (0)