Memory of a string literal is written through a pointer to it

tx0/c:string-literal-writedefaultwarningbug

What it finds

char *s = "name"; s[0] = 'N';: string literals may be kept in read-only memory and shared, and writing to one is undefined; the program crashes or changes every use of the same text. char s[] = "name"; makes a copy that can be written.

The pointer must be a local variable initialized with a string literal and never changed or given away by its address in the function; a write is an assignment, increment or decrement through it, or a call of strcpy, strcat, memcpy, memset, sprintf or strncpy with it as the destination.

From the rule's source, string_literal_write.rs.

Example from the rule's tests
#include <string.h> void change(void){  char *name = "fixed";  char *other = "moved";  char copy[] = "copy";  const char *kept = "kept";  name[0] = 'F';tx0 reports: this writes to the string literal name points to  other = copy;  other[0] = 'M';  copy[0] = 'C';  (void)kept;} void copies(const char *src){  char *dst = "x";  strcpy(dst, src);tx0 reports: this writes to the string literal dst points to}
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:string-literal-write" = "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:string-literal-write] reason="..."

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