Skip to content

C++: Remove FPs in cpp/wrong-type-format-argument caused by no linker awareness #18613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cpp/ql/src/Likely Bugs/Format/WrongTypeFormatArguments.ql
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ where
not arg.isAffectedByMacro() and
not arg.isFromUninstantiatedTemplate(_) and
not actual.stripType() instanceof ErroneousType and
not arg.(Call).mayBeFromImplicitlyDeclaredFunction()
not arg.(Call).mayBeFromImplicitlyDeclaredFunction() and
// Make sure that the format function definition is consistent
count(ffc.getTarget().getFormatParameterIndex()) = 1
select arg,
"This format specifier for type '" + expected.getName() + "' does not match the argument type '" +
actual.getUnspecifiedType().getName() + "'."
4 changes: 4 additions & 0 deletions cpp/ql/src/change-notes/2025-01-31-format-args.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* 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.
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,21 @@ void f(UNKNOWN_CHAR * str) {
fprintf(0, "%s", ""); // GOOD
printf("%s", str); // GOOD - erroneous type is ignored
}

#define va_list void*
#define va_start(x, y) x = 0;
#define va_arg(x, y) ((y)x)
#define va_end(x)
int vprintf(const char * format, va_list args);

int my_printf(const char * format, ...) {
va_list args;
va_start(args, format);
int result = vprintf(format, args);
va_end(args);
return result;
}

void linker_awareness_test() {
my_printf("%s%d", "", 1); // GOOD
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#define va_list void*
#define va_start(x, y) x = 0;
#define va_arg(x, y) ((y)x)
#define va_end(x)

int vprintf(const char * format, va_list args);

int my_printf(void * p,const char * format, ...) {
va_list args;
va_start(args, format);
int result = vprintf(format, args);
va_end(args);
return result;
}