Skip to content

Commit 94dea9f

Browse files
committed
C++: Add a test of unknown format specifiers (with specsAreKnown check disabled).
1 parent ceaaac2 commit 94dea9f

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

cpp/ql/src/semmle/code/cpp/commons/Printf.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ class FormatLiteral extends Literal {
921921
* format specifiers are present in the format string).
922922
*/
923923
predicate specsAreKnown() {
924-
this.getNumConvSpec() = count(int n | exists(this.getNumArgNeeded(n)))
924+
any()
925925
}
926926

927927
/**

cpp/ql/test/query-tests/Likely Bugs/Format/WrongNumberOfFormatArguments/TooManyFormatArguments.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@
1212
| test.c:39:3:39:8 | call to printf | Format expects 2 arguments but given 5 |
1313
| test.c:40:3:40:8 | call to printf | Format expects 2 arguments but given 4 |
1414
| test.c:41:3:41:8 | call to printf | Format expects 2 arguments but given 3 |
15+
| test.c:46:2:46:7 | call to printf | Format expects 1 arguments but given 2 |
16+
| test.c:47:2:47:7 | call to printf | Format expects 1 arguments but given 2 |
17+
| test.c:48:2:48:7 | call to printf | Format expects 1 arguments but given 2 |

cpp/ql/test/query-tests/Likely Bugs/Format/WrongNumberOfFormatArguments/test.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ void test(int i, const char *str)
4242
}
4343

4444
printf("%@ %i %i", 1, 2); // GOOD
45+
46+
printf("%Y", 1, 2); // GOOD (unknown format character, this might be correct) [FALSE POSITIVE]
47+
printf("%1.1Y", 1, 2); // GOOD (unknown format character, this might be correct) [FALSE POSITIVE]
48+
printf("%*.*Y", 1, 2); // GOOD (unknown format character, this might be correct) [FALSE POSITIVE]
4549
}

0 commit comments

Comments
 (0)