Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 76780d7

Browse files
committed
C++: Unify four implementations of repeatStars. Note that the recursive approach is faster for very large strings (well over 100 stars), while the concat approach appears to be faster for short strings and does not require an upper bound.
1 parent 95db7aa commit 76780d7

4 files changed

Lines changed: 12 additions & 39 deletions

File tree

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ private import semmle.code.cpp.dataflow.ExternalFlow
1212
private import semmle.code.cpp.ir.IR
1313

1414
module Input implements InputSig<DataFlowImplSpecific::CppDataFlow> {
15-
/**
16-
* Gets a string representing a level of indirection, for example for
17-
* `indirection = 2`, the result is `**`.
18-
*/
19-
private bindingset[indirection] string indirectionString(int indirection) {
20-
result = concat(int i | i in [1 .. indirection] | "*")
21-
}
22-
2315
class SummarizedCallableBase = Function;
2416

2517
ArgumentPosition callbackSelfParameterPosition() { result = TDirectPosition(-1) }
@@ -33,15 +25,15 @@ module Input implements InputSig<DataFlowImplSpecific::CppDataFlow> {
3325
string encodeReturn(ReturnKind rk, string arg) {
3426
rk != getStandardReturnValueKind() and
3527
result = "ReturnValue" and
36-
arg = indirectionString(rk.(NormalReturnKind).getIndirectionIndex())
28+
arg = repeatStars(rk.(NormalReturnKind).getIndirectionIndex())
3729
}
3830

3931
string encodeContent(ContentSet cs, string arg) {
4032
exists(FieldContent c |
4133
cs.isSingleton(c) and
4234
// FieldContent indices have 0 for the address, 1 for content, so we need to subtract one.
4335
result = "Field" and
44-
arg = indirectionString(c.getIndirectionIndex() - 1) + c.getField().getName()
36+
arg = repeatStars(c.getIndirectionIndex() - 1) + c.getField().getName()
4537
)
4638
}
4739

@@ -58,7 +50,7 @@ module Input implements InputSig<DataFlowImplSpecific::CppDataFlow> {
5850
*/
5951
private bindingset[argString] TPosition decodePosition(string argString) {
6052
exists(int indirection, string posString, int pos |
61-
argString = indirectionString(indirection) + posString and
53+
argString = repeatStars(indirection) + posString and
6254
pos = AccessPath::parseInt(posString) and
6355
(
6456
pos >= 0 and indirection = 0 and result = TDirectPosition(pos)
@@ -98,7 +90,7 @@ module Input implements InputSig<DataFlowImplSpecific::CppDataFlow> {
9890
result.isSingleton(c) and
9991
token.getName() = c.getField().getName() and
10092
// FieldContent indices have 0 for the address, 1 for content, so we need to subtract one.
101-
token.getAnArgument() = indirectionString(c.getIndirectionIndex() - 1)
93+
token.getAnArgument() = repeatStars(c.getIndirectionIndex() - 1)
10294
)
10395
}
10496
}

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,6 @@ module NodeStars {
8080
result = n.(FinalParameterNode).getIndirectionIndex()
8181
}
8282

83-
private int maxNumberOfIndirections() { result = max(getNumberOfIndirections(_)) }
84-
85-
private string repeatStars(int n) {
86-
n = 0 and result = ""
87-
or
88-
n = [1 .. maxNumberOfIndirections()] and
89-
result = "*" + repeatStars(n - 1)
90-
}
91-
9283
/**
9384
* Gets the number of stars (i.e., `*`s) needed to produce the `toString`
9485
* output for `n`.

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,16 +2363,15 @@ class Content extends TContent {
23632363
abstract predicate impliesClearOf(Content c);
23642364
}
23652365

2366-
private module ContentStars {
2367-
private int maxNumberOfIndirections() { result = max(any(Content c).getIndirectionIndex()) }
2368-
2369-
private string repeatStars(int n) {
2370-
n = 0 and result = ""
2371-
or
2372-
n = [1 .. maxNumberOfIndirections()] and
2373-
result = "*" + repeatStars(n - 1)
2374-
}
2366+
/**
2367+
* Gets a string consisting of `n` star characters ("*"), where n >= 0. This is
2368+
* used to represent indirection.
2369+
*/
2370+
bindingset[n] string repeatStars(int n) {
2371+
result = concat(int i | i in [1 .. n] | "*")
2372+
}
23752373

2374+
private module ContentStars {
23762375
/**
23772376
* Gets the number of stars (i.e., `*`s) needed to produce the `toString`
23782377
* output for `c`.

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternals.qll

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@ private module SourceVariables {
1919
ind = [0 .. countIndirectionsForCppType(base.getLanguageType()) + 1]
2020
}
2121

22-
private int maxNumberOfIndirections() { result = max(SourceVariable sv | | sv.getIndirection()) }
23-
24-
private string repeatStars(int n) {
25-
n = 0 and result = ""
26-
or
27-
n = [1 .. maxNumberOfIndirections()] and
28-
result = "*" + repeatStars(n - 1)
29-
}
30-
3122
class SourceVariable extends TSourceVariable {
3223
BaseSourceVariable base;
3324
int ind;

0 commit comments

Comments
 (0)