Skip to content

Commit defa4cc

Browse files
authored
Merge pull request github#18194 from github/calumgrant/bmn/wrong-number-format-arguments2
C++: Fix a FP in cpp/wrong-number-format-arguments caused by an extraction error
2 parents 9715ffd + 2da3d36 commit defa4cc

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

cpp/ql/src/Likely Bugs/Format/WrongNumberOfFormatArguments.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ where
4444
) and
4545
// A typical problem is that string literals are concatenated, but if one of the string
4646
// literals is an undefined macro, then this just leads to a syntax error.
47-
not exists(SyntaxError e | e.affects(fl))
47+
not exists(SyntaxError e | e.affects(fl)) and
48+
not ffc.getArgument(_) instanceof ErrorExpr
4849
select ffc,
4950
"Format for " + ffcName + " expects " + expected.toString() + " arguments but given " +
5051
given.toString()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The "Too few arguments to formatting function" query (`cpp/wrong-number-format-arguments`) query no longer produces results if an argument has an extraction error.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
| macros.cpp:14:2:14:37 | call to printf | Format for printf (in a macro expansion) expects 4 arguments but given 3 |
66
| macros.cpp:21:2:21:36 | call to printf | Format for printf (in a macro expansion) expects 4 arguments but given 3 |
77
| macros.cpp:32:2:32:25 | call to printf | Format for printf (in a macro expansion) expects 1 arguments but given 0 |
8+
| syntax_errors.c:15:5:15:10 | call to printf | Format for printf expects 2 arguments but given 0 |
89
| test.c:9:2:9:7 | call to printf | Format for printf expects 1 arguments but given 0 |
910
| test.c:12:2:12:7 | call to printf | Format for printf expects 2 arguments but given 1 |
1011
| test.c:15:2:15:7 | call to printf | Format for printf expects 3 arguments but given 2 |

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,16 @@
33
extern int printf(const char *fmt, ...);
44

55
void test_syntax_error() {
6-
printf("Error code %d: " FMT_MSG, 0, "");
6+
// GOOD
7+
printf("Error code %d: " UNDEFINED_MACRO, 0, "");
8+
9+
// GOOD
10+
printf("%d%d",
11+
(UNDEFINED_MACRO)1,
12+
(UNDEFINED_MACRO)2);
13+
14+
// GOOD [FALSE POSITIVE]
15+
printf("%d%d"
16+
UNDEFINED_MACRO,
17+
1, 2);
718
}

0 commit comments

Comments
 (0)