|
4 | 4 | * To use this library, extend the abstract class `InterestingPrefix` to have the library identify expressions that
|
5 | 5 | * may be appended to it, then check `InterestingPrefix.getAnAppendedExpression(Expr)` to get your results.
|
6 | 6 | *
|
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 | + * ``` |
9 | 27 | */
|
10 | 28 |
|
11 | 29 | import java
|
12 | 30 | import semmle.code.java.dataflow.TaintTracking
|
13 | 31 | private import semmle.code.java.StringFormat
|
14 | 32 |
|
| 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 | + */ |
15 | 39 | abstract class InterestingPrefix extends CompileTimeConstantExpr {
|
16 | 40 | /**
|
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. |
18 | 42 | */
|
19 | 43 | abstract int getOffset();
|
20 | 44 |
|
| 45 | + /** |
| 46 | + * Gets an expression that may follow this prefix in a derived string. |
| 47 | + */ |
21 | 48 | Expr getAnAppendedExpression() { mayFollowInterestingPrefix(this, result) }
|
22 | 49 | }
|
23 | 50 |
|
|
0 commit comments