Skip to content

Commit 3c25301

Browse files
committed
Extend documentation
1 parent d0d17e3 commit 3c25301

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

java/ql/lib/semmle/code/java/dataflow/StringPrefixes.qll

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,47 @@
44
* To use this library, extend the abstract class `InterestingPrefix` to have the library identify expressions that
55
* may be appended to it, then check `InterestingPrefix.getAnAppendedExpression(Expr)` to get your results.
66
*
7-
* For example, `private class FooPrefix extends InterestingPrefix { FooPrefix() { this = "foo:" } };`
8-
* `predicate mayFollowFoo(Expr e) { e = any(FooPrefix fp).getAnAppendedExpression() }`
7+
* For example, to identify expressions that may follow "foo:" in some string, we could define:
8+
*
9+
* ```
10+
* private class FooPrefix extends InterestingPrefix {
11+
* int offset;
12+
* FooPrefix() { this.getStringValue().substring("foo:") = offset };
13+
* override int getOffset() { result = offset }
14+
* };
15+
*
16+
* predicate mayFollowFoo(Expr e) { e = any(FooPrefix fp).getAnAppendedExpression() }
17+
* ```
18+
*
19+
* This will identify all the `suffix` expressions in contexts such as:
20+
*
21+
* ```
22+
* "foo:" + suffix1
23+
* "barfoo:" + suffix2
24+
* stringBuilder.append("foo:").append(suffix3);
25+
* String.format("%sfoo:%s", notSuffix, suffix4);
26+
* ```
927
*/
1028

1129
import java
1230
import semmle.code.java.dataflow.TaintTracking
1331
private import semmle.code.java.StringFormat
1432

33+
/**
34+
* A string constant that contains a prefix whose possible successor strings are returned
35+
* by `getAnAppendedExpression`.
36+
*
37+
* Extend this class to specify prefixes whose successors should be analysed.
38+
*/
1539
abstract class InterestingPrefix extends CompileTimeConstantExpr {
1640
/**
17-
* Gets the offset in this constant string where the interesting substring begins.
41+
* Gets the offset in this constant string where the interesting prefix begins.
1842
*/
1943
abstract int getOffset();
2044

45+
/**
46+
* Gets an expression that may follow this prefix in a derived string.
47+
*/
2148
Expr getAnAppendedExpression() { mayFollowInterestingPrefix(this, result) }
2249
}
2350

0 commit comments

Comments
 (0)