...
In this noncompliant code example, the char pointer p is initialized to the address of a string literal. Attempting to modify the string literal results in undefined behavior.:
| Code Block | ||||
|---|---|---|---|---|
| ||||
char *p = "string literal"; p[0] = 'S'; |
...
In this noncompliant code example, a string literal is passed to the (pointer to non-const) parameter of the POSIX function mkstemp(), which then modifies the characters of the string literal.:
| Code Block | ||||
|---|---|---|---|---|
| ||||
char *fname;
fname = mkstemp("/tmp/edXXXXXX");
|
...
Tool | Version | Checker | Description | ||||||
|---|---|---|---|---|---|---|---|---|---|
Compass/ROSE | Can detect simple violations of this rule. | ||||||||
| 157 S | Partially implemented. | |||||||
| PRQA QA-C |
| 0556 | Partially implemented. | ||||||
Splint |
|
|
...
| CERT C++ Secure Coding Standard | STR30-CPP. Do not attempt to modify string literals |
| ISO/IEC TS 17961 (Draft) | Modifying string literals [strmod] |
Bibliography
| [Plum 1991] | Topic 1.26, "Strings—String Literals" | ||
| [Summit 1995] | comp.lang.c FAQ list, Question 1.32 | [Plum 1991] | Topic 1.26, "Strings—String Literals" |
...