A format string of printf and its arguments do not match

tx0/c:format-argumentsdefaultwarningbug

What it finds

printf("%s: %d\n", name): the format asks for two arguments and gets one, so printf reads whatever happens to be where the second would be, which is undefined behavior. An argument the format never uses is ignored, which is as likely a mistake.

Checked are printf, fprintf, dprintf, sprintf and snprintf of the C library, which the file neither declares nor defines as a macro, with a format written as string literals only. A format with a conversion outside the C standard and POSIX, such as the extensions of the Linux kernel's printk, with positional arguments such as %1$s, or with a macro in it, as in "%" PRIu64, is not counted. %m of the GNU C library takes no argument. Too few arguments are reported only if none of them may be a macro that stands for several, as a name not declared in the file or a call of one may be.

From the rule's source, format_arguments.rs.

Example from the rule's tests
#include <stdio.h>#include <inttypes.h> void report(FILE *f, const char *name, int count, char *buf, size_t size, long id){  printf("%s has %d items\n", name);tx0 reports: the format asks for 2 arguments but printf gets 1  fprintf(f, "%s\n", name, count);tx0 reports: the format uses 1 argument but fprintf gets 2  snprintf(buf, size, "%d-%d", count);tx0 reports: asks for 2 arguments but snprintf gets 1  sprintf(buf, "%*d|%.*s", count, count);tx0 reports: asks for 4 arguments but sprintf gets 2  printf("100%%\n");  printf("%s: %m\n", name);  printf("%-10s|%08.3f|%lld|%zu\n", name, 1.5, (long long)id, size);  printf("%" PRId64 "\n", id);  printf("%1$s %1$s\n", name);  printf("%pS\n", name);  printf("%k\n", count);  printf(name, count);  printf("a" "%d" "b\n", count);  printf("a" "%d" "%d\n", count);tx0 reports: asks for 2 arguments but printf gets 1}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/c:format-arguments" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/c:format-arguments] reason="..."

tx0 explain tx0/c:format-arguments shows the same in the terminal. Found a false positive? Report it.