Skip to content

Commit 7fcbdbe

Browse files
committed
Shared: sync AccessPathSyntax.qll and FlowSummaryImpl.qll
1 parent d911e0a commit 7fcbdbe

File tree

5 files changed

+60
-52
lines changed

5 files changed

+60
-52
lines changed

csharp/ql/lib/semmle/code/csharp/dataflow/internal/AccessPathSyntax.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module AccessPath {
1919
private string getRawToken(AccessPath path, int n) {
2020
// Avoid splitting by '.' since tokens may contain dots, e.g. `Field[foo.Bar.x]`.
2121
// Instead use regexpFind to match valid tokens, and supplement with a final length
22-
// check to ensure all characters were included in a token.
22+
// check (in `AccessPath.hasSyntaxError`) to ensure all characters were included in a token.
2323
result = path.regexpFind("\\w+(?:\\[[^\\]]*\\])?(?=\\.|$)", n, _)
2424
}
2525

@@ -39,17 +39,17 @@ class AccessPath extends string instanceof AccessPath::Range {
3939
/** Gets the `n`th token on the access path (if there are no syntax errors). */
4040
AccessPathToken getToken(int n) {
4141
result = getRawToken(this, n) and
42-
not hasSyntaxError()
42+
not this.hasSyntaxError()
4343
}
4444

4545
/** Gets the number of tokens on the path (if there are no syntax errors). */
4646
int getNumToken() {
4747
result = count(int n | exists(getRawToken(this, n))) and
48-
not hasSyntaxError()
48+
not this.hasSyntaxError()
4949
}
5050

5151
/** Gets the `n`th-last token, with 0 being the last token. */
52-
AccessPathToken getLastToken(int n) { result = getToken(getNumToken() - 1 - n) }
52+
AccessPathToken getLastToken(int n) { result = this.getToken(this.getNumToken() - 1 - n) }
5353
}
5454

5555
/**

csharp/ql/lib/semmle/code/csharp/dataflow/internal/FlowSummaryImpl.qll

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -846,24 +846,26 @@ module Private {
846846
* Holds if `spec` specifies summary component stack `stack`.
847847
*/
848848
predicate interpretSpec(AccessPath spec, SummaryComponentStack stack) {
849-
interpretSpec(spec, 0, stack)
849+
interpretSpec(spec, spec.getNumToken(), stack)
850850
}
851851

852-
private predicate interpretSpec(AccessPath spec, int idx, SummaryComponentStack stack) {
853-
idx = spec.getNumToken() - 1 and
854-
stack = SummaryComponentStack::singleton(interpretComponent(spec.getLastToken(idx)))
852+
/** Holds if the first `n` tokens of `spec` resolves to `stack`. */
853+
private predicate interpretSpec(AccessPath spec, int n, SummaryComponentStack stack) {
854+
n = 1 and
855+
stack = SummaryComponentStack::singleton(interpretComponent(spec.getToken(0)))
855856
or
856857
exists(SummaryComponent head, SummaryComponentStack tail |
857-
interpretSpec(spec, idx, head, tail) and
858+
interpretSpec(spec, n, head, tail) and
858859
stack = SummaryComponentStack::push(head, tail)
859860
)
860861
}
861862

863+
/** Holds if the first `n` tokens of `spec` resolves to `head` followed by `tail` */
862864
private predicate interpretSpec(
863-
AccessPath output, int idx, SummaryComponent head, SummaryComponentStack tail
865+
AccessPath spec, int n, SummaryComponent head, SummaryComponentStack tail
864866
) {
865-
interpretSpec(output, idx + 1, tail) and
866-
head = interpretComponent(output.getLastToken(idx))
867+
interpretSpec(spec, n - 1, tail) and
868+
head = interpretComponent(spec.getToken(n - 1))
867869
}
868870

869871
private class MkStack extends RequiredSummaryComponentStack {
@@ -924,11 +926,12 @@ module Private {
924926
)
925927
}
926928

929+
/** Holds if the first `n` tokens of `output` resolve to the given interpretation. */
927930
private predicate interpretOutput(
928-
AccessPath output, int idx, InterpretNode ref, InterpretNode node
931+
AccessPath output, int n, InterpretNode ref, InterpretNode node
929932
) {
930933
sourceElementRef(ref, output, _) and
931-
idx = output.getNumToken() and
934+
n = 0 and
932935
(
933936
if output = ""
934937
then
@@ -938,8 +941,8 @@ module Private {
938941
)
939942
or
940943
exists(InterpretNode mid, AccessPathToken c |
941-
interpretOutput(output, idx + 1, ref, mid) and
942-
c = output.getLastToken(idx)
944+
interpretOutput(output, n - 1, ref, mid) and
945+
c = output.getToken(n - 1)
943946
|
944947
exists(ArgumentPosition apos, ParameterPosition ppos |
945948
node.asNode().(PostUpdateNode).getPreUpdateNode().(ArgNode).argumentOf(mid.asCall(), apos) and
@@ -962,11 +965,12 @@ module Private {
962965
)
963966
}
964967

968+
/** Holds if the first `n` tokens of `input` resolve to the given interpretation. */
965969
private predicate interpretInput(
966-
AccessPath input, int idx, InterpretNode ref, InterpretNode node
970+
AccessPath input, int n, InterpretNode ref, InterpretNode node
967971
) {
968972
sinkElementRef(ref, input, _) and
969-
idx = input.getNumToken() and
973+
n = 0 and
970974
(
971975
if input = ""
972976
then
@@ -976,8 +980,8 @@ module Private {
976980
)
977981
or
978982
exists(InterpretNode mid, AccessPathToken c |
979-
interpretInput(input, idx + 1, ref, mid) and
980-
c = input.getLastToken(idx)
983+
interpretInput(input, n - 1, ref, mid) and
984+
c = input.getToken(n - 1)
981985
|
982986
exists(ArgumentPosition apos, ParameterPosition ppos |
983987
node.asNode().(ArgNode).argumentOf(mid.asCall(), apos) and
@@ -1002,9 +1006,9 @@ module Private {
10021006
* model.
10031007
*/
10041008
predicate isSourceNode(InterpretNode node, string kind) {
1005-
exists(InterpretNode ref, string output |
1009+
exists(InterpretNode ref, AccessPath output |
10061010
sourceElementRef(ref, output, kind) and
1007-
interpretOutput(output, 0, ref, node)
1011+
interpretOutput(output, output.getNumToken(), ref, node)
10081012
)
10091013
}
10101014

@@ -1013,9 +1017,9 @@ module Private {
10131017
* model.
10141018
*/
10151019
predicate isSinkNode(InterpretNode node, string kind) {
1016-
exists(InterpretNode ref, string input |
1020+
exists(InterpretNode ref, AccessPath input |
10171021
sinkElementRef(ref, input, kind) and
1018-
interpretInput(input, 0, ref, node)
1022+
interpretInput(input, input.getNumToken(), ref, node)
10191023
)
10201024
}
10211025
}

javascript/ql/lib/semmle/javascript/frameworks/data/internal/AccessPathSyntax.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module AccessPath {
1919
private string getRawToken(AccessPath path, int n) {
2020
// Avoid splitting by '.' since tokens may contain dots, e.g. `Field[foo.Bar.x]`.
2121
// Instead use regexpFind to match valid tokens, and supplement with a final length
22-
// check to ensure all characters were included in a token.
22+
// check (in `AccessPath.hasSyntaxError`) to ensure all characters were included in a token.
2323
result = path.regexpFind("\\w+(?:\\[[^\\]]*\\])?(?=\\.|$)", n, _)
2424
}
2525

@@ -39,17 +39,17 @@ class AccessPath extends string instanceof AccessPath::Range {
3939
/** Gets the `n`th token on the access path (if there are no syntax errors). */
4040
AccessPathToken getToken(int n) {
4141
result = getRawToken(this, n) and
42-
not hasSyntaxError()
42+
not this.hasSyntaxError()
4343
}
4444

4545
/** Gets the number of tokens on the path (if there are no syntax errors). */
4646
int getNumToken() {
4747
result = count(int n | exists(getRawToken(this, n))) and
48-
not hasSyntaxError()
48+
not this.hasSyntaxError()
4949
}
5050

5151
/** Gets the `n`th-last token, with 0 being the last token. */
52-
AccessPathToken getLastToken(int n) { result = getToken(getNumToken() - 1 - n) }
52+
AccessPathToken getLastToken(int n) { result = this.getToken(this.getNumToken() - 1 - n) }
5353
}
5454

5555
/**

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module AccessPath {
1919
private string getRawToken(AccessPath path, int n) {
2020
// Avoid splitting by '.' since tokens may contain dots, e.g. `Field[foo.Bar.x]`.
2121
// Instead use regexpFind to match valid tokens, and supplement with a final length
22-
// check to ensure all characters were included in a token.
22+
// check (in `AccessPath.hasSyntaxError`) to ensure all characters were included in a token.
2323
result = path.regexpFind("\\w+(?:\\[[^\\]]*\\])?(?=\\.|$)", n, _)
2424
}
2525

@@ -39,17 +39,17 @@ class AccessPath extends string instanceof AccessPath::Range {
3939
/** Gets the `n`th token on the access path (if there are no syntax errors). */
4040
AccessPathToken getToken(int n) {
4141
result = getRawToken(this, n) and
42-
not hasSyntaxError()
42+
not this.hasSyntaxError()
4343
}
4444

4545
/** Gets the number of tokens on the path (if there are no syntax errors). */
4646
int getNumToken() {
4747
result = count(int n | exists(getRawToken(this, n))) and
48-
not hasSyntaxError()
48+
not this.hasSyntaxError()
4949
}
5050

5151
/** Gets the `n`th-last token, with 0 being the last token. */
52-
AccessPathToken getLastToken(int n) { result = getToken(getNumToken() - 1 - n) }
52+
AccessPathToken getLastToken(int n) { result = this.getToken(this.getNumToken() - 1 - n) }
5353
}
5454

5555
/**

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

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -846,24 +846,26 @@ module Private {
846846
* Holds if `spec` specifies summary component stack `stack`.
847847
*/
848848
predicate interpretSpec(AccessPath spec, SummaryComponentStack stack) {
849-
interpretSpec(spec, 0, stack)
849+
interpretSpec(spec, spec.getNumToken(), stack)
850850
}
851851

852-
private predicate interpretSpec(AccessPath spec, int idx, SummaryComponentStack stack) {
853-
idx = spec.getNumToken() - 1 and
854-
stack = SummaryComponentStack::singleton(interpretComponent(spec.getLastToken(idx)))
852+
/** Holds if the first `n` tokens of `spec` resolves to `stack`. */
853+
private predicate interpretSpec(AccessPath spec, int n, SummaryComponentStack stack) {
854+
n = 1 and
855+
stack = SummaryComponentStack::singleton(interpretComponent(spec.getToken(0)))
855856
or
856857
exists(SummaryComponent head, SummaryComponentStack tail |
857-
interpretSpec(spec, idx, head, tail) and
858+
interpretSpec(spec, n, head, tail) and
858859
stack = SummaryComponentStack::push(head, tail)
859860
)
860861
}
861862

863+
/** Holds if the first `n` tokens of `spec` resolves to `head` followed by `tail` */
862864
private predicate interpretSpec(
863-
AccessPath output, int idx, SummaryComponent head, SummaryComponentStack tail
865+
AccessPath spec, int n, SummaryComponent head, SummaryComponentStack tail
864866
) {
865-
interpretSpec(output, idx + 1, tail) and
866-
head = interpretComponent(output.getLastToken(idx))
867+
interpretSpec(spec, n - 1, tail) and
868+
head = interpretComponent(spec.getToken(n - 1))
867869
}
868870

869871
private class MkStack extends RequiredSummaryComponentStack {
@@ -924,11 +926,12 @@ module Private {
924926
)
925927
}
926928

929+
/** Holds if the first `n` tokens of `output` resolve to the given interpretation. */
927930
private predicate interpretOutput(
928-
AccessPath output, int idx, InterpretNode ref, InterpretNode node
931+
AccessPath output, int n, InterpretNode ref, InterpretNode node
929932
) {
930933
sourceElementRef(ref, output, _) and
931-
idx = output.getNumToken() and
934+
n = 0 and
932935
(
933936
if output = ""
934937
then
@@ -938,8 +941,8 @@ module Private {
938941
)
939942
or
940943
exists(InterpretNode mid, AccessPathToken c |
941-
interpretOutput(output, idx + 1, ref, mid) and
942-
c = output.getLastToken(idx)
944+
interpretOutput(output, n - 1, ref, mid) and
945+
c = output.getToken(n - 1)
943946
|
944947
exists(ArgumentPosition apos, ParameterPosition ppos |
945948
node.asNode().(PostUpdateNode).getPreUpdateNode().(ArgNode).argumentOf(mid.asCall(), apos) and
@@ -962,11 +965,12 @@ module Private {
962965
)
963966
}
964967

968+
/** Holds if the first `n` tokens of `input` resolve to the given interpretation. */
965969
private predicate interpretInput(
966-
AccessPath input, int idx, InterpretNode ref, InterpretNode node
970+
AccessPath input, int n, InterpretNode ref, InterpretNode node
967971
) {
968972
sinkElementRef(ref, input, _) and
969-
idx = input.getNumToken() and
973+
n = 0 and
970974
(
971975
if input = ""
972976
then
@@ -976,8 +980,8 @@ module Private {
976980
)
977981
or
978982
exists(InterpretNode mid, AccessPathToken c |
979-
interpretInput(input, idx + 1, ref, mid) and
980-
c = input.getLastToken(idx)
983+
interpretInput(input, n - 1, ref, mid) and
984+
c = input.getToken(n - 1)
981985
|
982986
exists(ArgumentPosition apos, ParameterPosition ppos |
983987
node.asNode().(ArgNode).argumentOf(mid.asCall(), apos) and
@@ -1002,9 +1006,9 @@ module Private {
10021006
* model.
10031007
*/
10041008
predicate isSourceNode(InterpretNode node, string kind) {
1005-
exists(InterpretNode ref, string output |
1009+
exists(InterpretNode ref, AccessPath output |
10061010
sourceElementRef(ref, output, kind) and
1007-
interpretOutput(output, 0, ref, node)
1011+
interpretOutput(output, output.getNumToken(), ref, node)
10081012
)
10091013
}
10101014

@@ -1013,9 +1017,9 @@ module Private {
10131017
* model.
10141018
*/
10151019
predicate isSinkNode(InterpretNode node, string kind) {
1016-
exists(InterpretNode ref, string input |
1020+
exists(InterpretNode ref, AccessPath input |
10171021
sinkElementRef(ref, input, kind) and
1018-
interpretInput(input, 0, ref, node)
1022+
interpretInput(input, input.getNumToken(), ref, node)
10191023
)
10201024
}
10211025
}

0 commit comments

Comments
 (0)