A string literal longer than an array is copied into it with strcpy or sprintf

tx0/c:literal-copy-overflows-arraydefaultwarningreliability/memory-safetydepth fastbug, cwe-787

What it finds

char code[4]; strcpy(code, "ABCD");: the literal needs one byte more than its characters for the terminating null, so copying it writes past the end of the array.

The array must be a variable declared in the file with a character type and a literal length; sprintf counts only with a format without %.

From the rule's source, literal_copy_overflows_array.rs.

Example from the rule's tests
#include <string.h> void label(void){  char code[4];  strcpy(code, "ABCD");tx0 reports: strcpy writes 5 bytes into code, which holds 4  strcpy(code, "ABC");}
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:literal-copy-overflows-array" = "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:literal-copy-overflows-array] reason="..."

tx0 explain tx0/c:literal-copy-overflows-array shows the same in the terminal. Found a false positive? Report it.