Skip to content

Commit 6a5f37b

Browse files
authored
Merge pull request github#11149 from geoffw0/wrong-number-msg
C++: Clearer messages for the format args queries
2 parents 8b11e98 + c842677 commit 6a5f37b

File tree

5 files changed

+58
-32
lines changed

5 files changed

+58
-32
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@
1313

1414
import cpp
1515

16-
from FormatLiteral fl, FormattingFunctionCall ffc, int expected, int given
16+
from FormatLiteral fl, FormattingFunctionCall ffc, int expected, int given, string ffcName
1717
where
1818
ffc = fl.getUse() and
1919
expected = fl.getNumArgNeeded() and
2020
given = ffc.getNumFormatArgument() and
2121
expected < given and
22-
fl.specsAreKnown()
23-
select ffc, "Format expects " + expected.toString() + " arguments but given " + given.toString()
22+
fl.specsAreKnown() and
23+
(
24+
if ffc.isInMacroExpansion()
25+
then ffcName = ffc.getTarget().getName() + " (in a macro expansion)"
26+
else ffcName = ffc.getTarget().getName()
27+
)
28+
select ffc,
29+
"Format for " + ffcName + " expects " + expected.toString() + " arguments but given " +
30+
given.toString()

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@
1616

1717
import cpp
1818

19-
from FormatLiteral fl, FormattingFunctionCall ffc, int expected, int given
19+
from FormatLiteral fl, FormattingFunctionCall ffc, int expected, int given, string ffcName
2020
where
2121
ffc = fl.getUse() and
2222
expected = fl.getNumArgNeeded() and
2323
given = ffc.getNumFormatArgument() and
2424
expected > given and
25-
fl.specsAreKnown()
26-
select ffc, "Format expects " + expected.toString() + " arguments but given " + given.toString()
25+
fl.specsAreKnown() and
26+
(
27+
if ffc.isInMacroExpansion()
28+
then ffcName = ffc.getTarget().getName() + " (in a macro expansion)"
29+
else ffcName = ffc.getTarget().getName()
30+
)
31+
select ffc,
32+
"Format for " + ffcName + " expects " + expected.toString() + " arguments but given " +
33+
given.toString()
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
| a.c:18:3:18:25 | call to myMultiplyDefinedPrintf | Format expects 1 arguments but given 2 |
2-
| b.c:15:3:15:25 | call to myMultiplyDefinedPrintf | Format expects 1 arguments but given 2 |
3-
| c.c:7:3:7:25 | call to myMultiplyDefinedPrintf | Format expects 1 arguments but given 2 |
4-
| custom_printf.cpp:31:5:31:12 | call to myPrintf | Format expects 2 arguments but given 3 |
5-
| macros.cpp:12:2:12:31 | call to printf | Format expects 2 arguments but given 3 |
6-
| macros.cpp:16:2:16:30 | call to printf | Format expects 2 arguments but given 3 |
7-
| test.c:7:2:7:7 | call to printf | Format expects 0 arguments but given 1 |
8-
| test.c:21:2:21:7 | call to printf | Format expects 2 arguments but given 3 |
9-
| test.c:27:3:27:8 | call to printf | Format expects 2 arguments but given 3 |
10-
| test.c:31:3:31:8 | call to printf | Format expects 1 arguments but given 3 |
11-
| test.c:32:3:32:8 | call to printf | Format expects 1 arguments but given 2 |
12-
| test.c:39:3:39:8 | call to printf | Format expects 2 arguments but given 5 |
13-
| test.c:40:3:40:8 | call to printf | Format expects 2 arguments but given 4 |
14-
| test.c:41:3:41:8 | call to printf | Format expects 2 arguments but given 3 |
1+
| a.c:18:3:18:25 | call to myMultiplyDefinedPrintf | Format for myMultiplyDefinedPrintf expects 1 arguments but given 2 |
2+
| b.c:15:3:15:25 | call to myMultiplyDefinedPrintf | Format for myMultiplyDefinedPrintf expects 1 arguments but given 2 |
3+
| c.c:7:3:7:25 | call to myMultiplyDefinedPrintf | Format for myMultiplyDefinedPrintf expects 1 arguments but given 2 |
4+
| custom_printf.cpp:31:5:31:12 | call to myPrintf | Format for myPrintf expects 2 arguments but given 3 |
5+
| macros.cpp:12:2:12:31 | call to printf | Format for printf (in a macro expansion) expects 2 arguments but given 3 |
6+
| macros.cpp:16:2:16:30 | call to printf | Format for printf (in a macro expansion) expects 2 arguments but given 3 |
7+
| test.c:7:2:7:7 | call to printf | Format for printf expects 0 arguments but given 1 |
8+
| test.c:21:2:21:7 | call to printf | Format for printf expects 2 arguments but given 3 |
9+
| test.c:27:3:27:8 | call to printf | Format for printf expects 2 arguments but given 3 |
10+
| test.c:31:3:31:8 | call to printf | Format for printf expects 1 arguments but given 3 |
11+
| test.c:32:3:32:8 | call to printf | Format for printf expects 1 arguments but given 2 |
12+
| test.c:39:3:39:8 | call to printf | Format for printf expects 2 arguments but given 5 |
13+
| test.c:40:3:40:8 | call to printf | Format for printf expects 2 arguments but given 4 |
14+
| test.c:41:3:41:8 | call to printf | Format for printf expects 2 arguments but given 3 |
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
| a.c:16:3:16:25 | call to myMultiplyDefinedPrintf | Format expects 1 arguments but given 0 |
2-
| b.c:13:3:13:25 | call to myMultiplyDefinedPrintf | Format expects 1 arguments but given 0 |
3-
| c.c:5:3:5:25 | call to myMultiplyDefinedPrintf | Format expects 1 arguments but given 0 |
4-
| custom_printf.cpp:29:5:29:12 | call to myPrintf | Format expects 2 arguments but given 1 |
5-
| macros.cpp:14:2:14:37 | call to printf | Format expects 4 arguments but given 3 |
6-
| macros.cpp:21:2:21:36 | call to printf | Format expects 4 arguments but given 3 |
7-
| test.c:9:2:9:7 | call to printf | Format expects 1 arguments but given 0 |
8-
| test.c:12:2:12:7 | call to printf | Format expects 2 arguments but given 1 |
9-
| test.c:15:2:15:7 | call to printf | Format expects 3 arguments but given 2 |
10-
| test.c:19:2:19:7 | call to printf | Format expects 2 arguments but given 1 |
11-
| test.c:29:3:29:8 | call to printf | Format expects 2 arguments but given 1 |
1+
| a.c:16:3:16:25 | call to myMultiplyDefinedPrintf | Format for myMultiplyDefinedPrintf expects 1 arguments but given 0 |
2+
| b.c:13:3:13:25 | call to myMultiplyDefinedPrintf | Format for myMultiplyDefinedPrintf expects 1 arguments but given 0 |
3+
| c.c:5:3:5:25 | call to myMultiplyDefinedPrintf | Format for myMultiplyDefinedPrintf expects 1 arguments but given 0 |
4+
| custom_printf.cpp:29:5:29:12 | call to myPrintf | Format for myPrintf expects 2 arguments but given 1 |
5+
| macros.cpp:14:2:14:37 | call to printf | Format for printf (in a macro expansion) expects 4 arguments but given 3 |
6+
| macros.cpp:21:2:21:36 | call to printf | Format for printf (in a macro expansion) expects 4 arguments but given 3 |
7+
| macros.cpp:32:2:32:25 | call to printf | Format for printf (in a macro expansion) expects 1 arguments but given 0 |
8+
| test.c:9:2:9:7 | call to printf | Format for printf expects 1 arguments but given 0 |
9+
| test.c:12:2:12:7 | call to printf | Format for printf expects 2 arguments but given 1 |
10+
| test.c:15:2:15:7 | call to printf | Format for printf expects 3 arguments but given 2 |
11+
| test.c:19:2:19:7 | call to printf | Format for printf expects 2 arguments but given 1 |
12+
| test.c:29:3:29:8 | call to printf | Format for printf expects 2 arguments but given 1 |

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,21 @@ void testMacros(int a, int b, int c)
1313
GOODPRINTF("%i %i %i\n", a, b, c); // GOOD
1414
GOODPRINTF("%i %i %i %i\n", a, b, c); // BAD: too few format arguments
1515

16-
BADPRINTF("%i %i\n", a, b, 0); // BAD: too many format arguments
16+
BADPRINTF("%i %i\n", a, b, 0); // DUBIOUS: too many format arguments
1717
// ^ here there are too many format arguments, but the design of the Macro forces the user
1818
// to do this, and the extra argument is harmlessly ignored in practice. Reporting these
1919
// results can be extremely noisy (e.g. in openldap).
2020
BADPRINTF("%i %i %i\n", a, b, c); // GOOD
2121
BADPRINTF("%i %i %i %i\n", a, b, c); // BAD: too few format arguments
2222
}
23+
24+
#define DOTHING(x) \
25+
printf("doing thing: " #x); x
26+
27+
void testMacros2()
28+
{
29+
int x;
30+
31+
DOTHING(x++); // GOOD
32+
DOTHING(printf("%i", x)); // BAD: the printf inside the macro has too few format arguments
33+
}

0 commit comments

Comments
 (0)