Skip to content

Commit ed3ad1a

Browse files
authored
Merge pull request github#18613 from github/calumgrant/bmn/wrong-type-format-arg-linkage
C++: Remove FPs in cpp/wrong-type-format-argument caused by no linker awareness
2 parents 1066b88 + 25d8f0e commit ed3ad1a

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ where
171171
not arg.isAffectedByMacro() and
172172
not arg.isFromUninstantiatedTemplate(_) and
173173
not actual.stripType() instanceof ErroneousType and
174-
not arg.(Call).mayBeFromImplicitlyDeclaredFunction()
174+
not arg.(Call).mayBeFromImplicitlyDeclaredFunction() and
175+
// Make sure that the format function definition is consistent
176+
count(ffc.getTarget().getFormatParameterIndex()) = 1
175177
select arg,
176178
"This format specifier for type '" + expected.getName() + "' does not match the argument type '" +
177179
actual.getUnspecifiedType().getName() + "'."
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 "Wrong type of arguments to formatting function" query (`cpp/wrong-type-format-argument`) now produces fewer FPs if the formatting function has multiple definitions.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,21 @@ void f(UNKNOWN_CHAR * str) {
1010
fprintf(0, "%s", ""); // GOOD
1111
printf("%s", str); // GOOD - erroneous type is ignored
1212
}
13+
14+
#define va_list void*
15+
#define va_start(x, y) x = 0;
16+
#define va_arg(x, y) ((y)x)
17+
#define va_end(x)
18+
int vprintf(const char * format, va_list args);
19+
20+
int my_printf(const char * format, ...) {
21+
va_list args;
22+
va_start(args, format);
23+
int result = vprintf(format, args);
24+
va_end(args);
25+
return result;
26+
}
27+
28+
void linker_awareness_test() {
29+
my_printf("%s%d", "", 1); // GOOD
30+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#define va_list void*
2+
#define va_start(x, y) x = 0;
3+
#define va_arg(x, y) ((y)x)
4+
#define va_end(x)
5+
6+
int vprintf(const char * format, va_list args);
7+
8+
int my_printf(void * p,const char * format, ...) {
9+
va_list args;
10+
va_start(args, format);
11+
int result = vprintf(format, args);
12+
va_end(args);
13+
return result;
14+
}

0 commit comments

Comments
 (0)