Skip to content

Commit 9a407eb

Browse files
committed
CPP: Test format args with mismatching declarations.
1 parent 72db219 commit 9a407eb

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
| a.c:14:3:14:25 | call to myMultiplyDefinedPrintf | Format expects 1 arguments but given 2 |
2+
| a.c:17:3:17:26 | call to myMultiplyDefinedPrintf2 | Format expects 1 arguments but given 2 |
3+
| b.c:11:3:11:25 | call to myMultiplyDefinedPrintf | Format expects 1 arguments but given 2 |
4+
| b.c:14:3:14:26 | call to myMultiplyDefinedPrintf2 | Format expects 1 arguments but given 2 |
5+
| c.c:7:3:7:25 | call to myMultiplyDefinedPrintf | Format expects 1 arguments but given 2 |
6+
| c.c:10:3:10:26 | call to myMultiplyDefinedPrintf2 | Format expects 1 arguments but given 2 |
17
| custom_printf.cpp:31:5:31:12 | call to myPrintf | Format expects 2 arguments but given 3 |
28
| macros.cpp:12:2:12:31 | call to printf | Format expects 2 arguments but given 3 |
39
| macros.cpp:16:2:16:30 | call to printf | Format expects 2 arguments but given 3 |

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
| a.c:12:3:12:25 | call to myMultiplyDefinedPrintf | Format expects 1 arguments but given 0 |
2+
| a.c:15:3:15:26 | call to myMultiplyDefinedPrintf2 | Format expects 1 arguments but given 0 |
3+
| b.c:9:3:9:25 | call to myMultiplyDefinedPrintf | Format expects 1 arguments but given 0 |
4+
| b.c:12:3:12:26 | call to myMultiplyDefinedPrintf2 | Format expects 1 arguments but given 0 |
5+
| c.c:5:3:5:25 | call to myMultiplyDefinedPrintf | Format expects 1 arguments but given 0 |
6+
| c.c:8:3:8:26 | call to myMultiplyDefinedPrintf2 | Format expects 1 arguments but given 0 |
17
| custom_printf.cpp:29:5:29:12 | call to myPrintf | Format expects 2 arguments but given 1 |
28
| macros.cpp:14:2:14:37 | call to printf | Format expects 4 arguments but given 3 |
39
| macros.cpp:21:2:21:36 | call to printf | Format expects 4 arguments but given 3 |
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
__attribute__((format(printf, 1, 3)))
3+
void myMultiplyDefinedPrintf(const char *format, int extraArg, ...)
4+
{
5+
// ...
6+
}
7+
__attribute__((format(printf, 1, 3)))
8+
void myMultiplyDefinedPrintf2(const char *format, int extraArg, ...);
9+
10+
void test_custom_printf1()
11+
{
12+
myMultiplyDefinedPrintf("%i", 0); // BAD (too few format arguments)
13+
myMultiplyDefinedPrintf("%i", 0, 1); // GOOD
14+
myMultiplyDefinedPrintf("%i", 0, 1, 2); // BAD (too many format arguments)
15+
myMultiplyDefinedPrintf2("%i", 0); // GOOD (we can't tell which definition is correct so we have to assume this is OK) [FALSE POSITIVE]
16+
myMultiplyDefinedPrintf2("%i", 0, 1); // GOOD (we can't tell which definition is correct so we have to assume this is OK)
17+
myMultiplyDefinedPrintf2("%i", 0, 1, 2); // GOOD (we can't tell which definition is correct so we have to assume this is OK) [FALSE POSITIVE]
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
__attribute__((format(printf, 1, 2)))
3+
void myMultiplyDefinedPrintf(const char *format, ...); // this declaration does not match the definition
4+
__attribute__((format(printf, 1, 2)))
5+
void myMultiplyDefinedPrintf2(const char *format, ...);
6+
7+
void test_custom_printf2()
8+
{
9+
myMultiplyDefinedPrintf("%i", 0); // BAD (too few format arguments)
10+
myMultiplyDefinedPrintf("%i", 0, 1); // GOOD
11+
myMultiplyDefinedPrintf("%i", 0, 1, 2); // BAD (too many format arguments)
12+
myMultiplyDefinedPrintf2("%i", 0); // GOOD (we can't tell which definition is correct so we have to assume this is OK) [FALSE POSITIVE]
13+
myMultiplyDefinedPrintf2("%i", 0, 1); // GOOD (we can't tell which definition is correct so we have to assume this is OK)
14+
myMultiplyDefinedPrintf2("%i", 0, 1, 2); // GOOD (we can't tell which definition is correct so we have to assume this is OK) [FALSE POSITIVE]
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
void test_custom_printf2()
3+
{
4+
// (implicitly defined)
5+
myMultiplyDefinedPrintf("%i", 0); // BAD (too few format arguments)
6+
myMultiplyDefinedPrintf("%i", 0, 1); // GOOD
7+
myMultiplyDefinedPrintf("%i", 0, 1, 2); // BAD (too many format arguments)
8+
myMultiplyDefinedPrintf2("%i", 0); // GOOD (we can't tell which definition is correct so we have to assume this is OK) [FALSE POSITIVE]
9+
myMultiplyDefinedPrintf2("%i", 0, 1); // GOOD (we can't tell which definition is correct so we have to assume this is OK)
10+
myMultiplyDefinedPrintf2("%i", 0, 1, 2); // GOOD (we can't tell which definition is correct so we have to assume this is OK) [FALSE POSITIVE]
11+
}

0 commit comments

Comments
 (0)