Skip to content

Commit 79e87a6

Browse files
authored
Merge pull request #4088 from aschackmull/java/string-formatted
Approved by aibaars
2 parents 4e963a8 + 28578fd commit 79e87a6

File tree

5 files changed

+19
-2
lines changed

5 files changed

+19
-2
lines changed

java/ql/src/semmle/code/java/StringFormat.qll

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class StringFormatMethod extends FormatMethod {
2222
StringFormatMethod() {
2323
(
2424
this.hasName("format") or
25+
this.hasName("formatted") or
2526
this.hasName("printf") or
2627
this.hasName("readLine") or
2728
this.hasName("readPassword")
@@ -38,6 +39,8 @@ class StringFormatMethod extends FormatMethod {
3839
override int getFormatStringIndex() {
3940
result = 0 and this.getSignature() = "format(java.lang.String,java.lang.Object[])"
4041
or
42+
result = -1 and this.getSignature() = "formatted(java.lang.Object[])"
43+
or
4144
result = 0 and this.getSignature() = "printf(java.lang.String,java.lang.Object[])"
4245
or
4346
result = 1 and
@@ -91,6 +94,12 @@ class FmtSyntax extends TFmtSyntax {
9194
predicate isLogger() { this = TFmtLogger() }
9295
}
9396

97+
private Expr getArgumentOrQualifier(Call c, int i) {
98+
result = c.getArgument(i)
99+
or
100+
result = c.getQualifier() and i = -1
101+
}
102+
94103
/**
95104
* Holds if `c` wraps a call to a `StringFormatMethod`, such that `fmtix` is
96105
* the index of the format string argument to `c` and the following and final
@@ -111,7 +120,7 @@ private predicate formatWrapper(Callable c, int fmtix, FmtSyntax syntax) {
111120
or
112121
fmtcall.getCallee().(LoggerFormatMethod).getFormatStringIndex() = i and syntax = TFmtLogger()
113122
) and
114-
fmtcall.getArgument(i) = fmt.getAnAccess() and
123+
getArgumentOrQualifier(fmtcall, i) = fmt.getAnAccess() and
115124
fmtcall.getArgument(i + 1) = args.getAnAccess()
116125
)
117126
}
@@ -155,7 +164,7 @@ class FormattingCall extends Call {
155164
}
156165

157166
/** Gets the argument to this call in the position of the format string */
158-
Expr getFormatArgument() { result = this.getArgument(this.getFormatStringIndex()) }
167+
Expr getFormatArgument() { result = getArgumentOrQualifier(this, this.getFormatStringIndex()) }
159168

160169
/** Gets an argument to be formatted. */
161170
Expr getAnArgumentToBeFormatted() {

java/ql/test/query-tests/StringFormat/A.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,9 @@ void g(boolean b, int i) {
8585
String.format("%s%s", a2); // ok
8686
String.format("%s", a2); // unused
8787
}
88+
89+
void formatted() {
90+
"%s%s".formatted(""); // missing
91+
"%s".formatted("", ""); // unused
92+
}
8893
}

java/ql/test/query-tests/StringFormat/MissingFormatArg.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
| A.java:74:5:74:47 | format(...) | This format call refers to 2 argument(s) but only supplies 1 argument(s). |
1818
| A.java:79:5:79:31 | format(...) | This format call refers to 3 argument(s) but only supplies 2 argument(s). |
1919
| A.java:84:5:84:31 | format(...) | This format call refers to 3 argument(s) but only supplies 2 argument(s). |
20+
| A.java:90:5:90:24 | formatted(...) | This format call refers to 2 argument(s) but only supplies 1 argument(s). |

java/ql/test/query-tests/StringFormat/UnusedFormatArg.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
| A.java:76:5:76:57 | format(...) | This format call refers to 2 argument(s) but supplies 3 argument(s). |
1010
| A.java:81:5:81:27 | format(...) | This format call refers to 1 argument(s) but supplies 2 argument(s). |
1111
| A.java:86:5:86:27 | format(...) | This format call refers to 1 argument(s) but supplies 2 argument(s). |
12+
| A.java:91:5:91:26 | formatted(...) | This format call refers to 1 argument(s) but supplies 2 argument(s). |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//semmle-extractor-options: --javac-args --enable-preview -source 14 -target 14

0 commit comments

Comments
 (0)