CPP: Test for WrongTypeFormatArguments with multiple definitions.

This commit is contained in:
Geoffrey White
2019-09-20 15:59:26 +01:00
parent 9986de87c4
commit 144cda7dd9
3 changed files with 34 additions and 0 deletions

View File

@@ -1,3 +1,7 @@
| a.c:13:39:13:42 | 1.0 | This argument should be of type 'char *' but is of type 'double' |
| a.c:13:39:13:42 | 1.0 | This argument should be of type 'int' but is of type 'double' |
| a.c:15:40:15:43 | 1.0 | This argument should be of type 'char *' but is of type 'double' |
| a.c:15:40:15:43 | 1.0 | This argument should be of type 'int' but is of type 'double' |
| format.h:16:59:16:61 | str | This argument should be of type 'int' but is of type 'char *' |
| format.h:16:64:16:64 | i | This argument should be of type 'double' but is of type 'int' |
| format.h:16:67:16:67 | d | This argument should be of type 'char *' but is of type 'double' |

View File

@@ -0,0 +1,16 @@
__attribute__((format(printf, 1, 3)))
void myMultiplyDefinedPrintf(const char *format, const char *extraArg, ...)
{
// ...
}
__attribute__((format(printf, 1, 3)))
void myMultiplyDefinedPrintf2(const char *format, const char *extraArg, ...);
void test_custom_printf1()
{
myMultiplyDefinedPrintf("%i", "%s", 1); // GOOD
myMultiplyDefinedPrintf("%i", "%s", 1.0f); // BAD
myMultiplyDefinedPrintf2("%i", "%s", 1); // GOOD (we can't tell which definition is correct so we have to assume this is OK)
myMultiplyDefinedPrintf2("%i", "%s", 1.0f); // GOOD (we can't tell which definition is correct so we have to assume this is OK) [FALSE POSITIVE]
}

View File

@@ -0,0 +1,14 @@
__attribute__((format(printf, 2, 3)))
void myMultiplyDefinedPrintf(const char *extraArg, const char *format, ...); // this declaration does not match the definition
__attribute__((format(printf, 2, 3)))
void myMultiplyDefinedPrintf2(const char *extraArg, const char *format, ...);
void test_custom_printf2()
{
myMultiplyDefinedPrintf("%i", "%f", 1); // GOOD
myMultiplyDefinedPrintf("%i", "%f", 1.0f); // BAD [NOT DETECTED]
myMultiplyDefinedPrintf2("%i", "%f", 1); // GOOD (we can't tell which definition is correct so we have to assume this is OK)
myMultiplyDefinedPrintf2("%i", "%f", 1.0f); // GOOD (we can't tell which definition is correct so we have to assume this is OK)
}