sprintf writes formatted text into a buffer whose size it does not know

tx0/c:unbounded-sprintfstrictwarningsecurity, cwe-120

What it finds

sprintf(buf, "%s: %s", user, msg) writes as much as the arguments make, however large buf is. snprintf with the size of the buffer cannot run past it.

A call is left alone when the buffer is an array the file declares with a literal length, the format a string literal, and the longest text the format can make fits: numbers and characters only, whose widths are bounded.

From the rule's source, unbounded_sprintf.rs.

Example from the rule's tests
#include <stdio.h> void format(char *out, const char *name, int n){  char num[16];  char tiny[4];  sprintf(out, "%s", name);tx0 reports: sprintf does not know how large its buffer is  sprintf(num, "%d", n);  sprintf(tiny, "%d", n);tx0 reports: sprintf does not know how large its buffer is  snprintf(out, 8, "%s", name);}
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 from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/c:unbounded-sprintf" = "warning"

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:unbounded-sprintf] reason="..."

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