The type of a narrow string literal is an array of char
, and the type of a wide string literal is an array of wchar_t
. However, string literals (of both types) are notionally constant and should consequently be protected by const
qualification. This recommendation is an extension of rule:a specialization of DCL00-C. Const-qualify immutable objects and also supports STR30-C. Do not attempt to modify string literals
As string literals are constant, they should only be assigned to constant pointers.
Non-Compliant Code Example 1
.
Adding const
qualification may propagate through a program; as const
qualifiers are added, still more become necessary. This phenomenon is sometimes called const-poisoning. Const-poisoning can frequently lead to violations of EXP05-C. Do not cast away a const qualification. Although const
qualification is a good idea, the costs may outweigh the value in the remediation of existing code.
Noncompliant Code Example (Narrow String Literal)
In this noncompliant code example, the const
keyword has been omitted:The const
keyword is not included in this declaration.
Code Block | ||||
---|---|---|---|---|
| ||||
char * c = "Hello"; // Bad: assigned to non-const c[3] = 'a'; // Undefined (but compiles) |
Compliant Solution 1
|
If a statement such as c[0] = 'C'
were placed following the declaration in the noncompliant code example, the code is likely to compile cleanly, but the result of the assignment would be undefined because string literals are considered constant.
Compliant Solution (Immutable Strings)
In this compliant solution, the characters referred to by the pointer c
are const
-qualified, meaning that any attempt to assign them to different values is an error:If you properly assign string literals to const
pointers, the compiler will not allow direct manipulation of the contents.
Code Block | ||||
---|---|---|---|---|
| ||||
const char * char* const c = "Hello"; // Good //c[3] = 'a'; would cause a compile error |
Aside
Note that the following code is acceptable, as a
and b
do not actually point to string literals. They are char
array objects which have had characters copied into them, and therefore are modifiable.
Compliant Solution (Mutable Strings)
In cases where the string is meant to be modified, use initialization instead of assignment. In this compliant solution, c
is a modifiable char
array that has been initialized using the contents of the corresponding string literal:
Code Block | ||||
---|---|---|---|---|
| ||||
char c | ||||
Code Block | ||||
char a[] = "abc"; char b[3] = "abcHello"; |
The above code is equivalent to:
Code Block |
---|
char a[] = {'a', 'b', 'c', '\0'};
char b[] = {'a', 'b', 'c'};
|
Non-Compliant Coding Example 2.a
Consequently, a statement such as c[0] = 'C'
is valid and behaves as expected.
Noncompliant Code Example (Wide String Literal)
In this noncompliant code example, the const
keyword has been omitted:Though it is not compliant with the C Standard, this code executes correctly if the contents of CMUfullname
are not modified.
Code Block | ||||
---|---|---|---|---|
| ||||
wchar_t *c char* CMUfullname = L"Carnegie Mellon"; /* get school from user input and validate */ if (strcmp(school,"CMU")) { school = CMUfullname; } |
Non-Compliant Coding Example 2.b
Adding in the const
keyword will generate a compiler warning, as the assignment of CMUfullname
to school
discards the const
qualifier. Any modifications to the contents of school
after this assignment will lead to errors.
Code Block | ||
---|---|---|
| ||
char* const CMUfullname = "Carnegie Mellon";
/* get school from user input and validate */
if (strcmp(school,"CMU")) {
school = CMUfullname;
}
|
Compliant Solution 2
The compliant solution uses the const
keyword to protect the string literal, as well as using strcpy
to copy the value of CMUfullname
into school
, allowing future modification of school
.
...
bgColor | #ccccFF |
---|
Hello";
|
If a statement such as c[0] = L'C'
were placed following this declaration, the code is likely to compile cleanly, but the result of the assignment would be undefined because string literals are considered constant.
Compliant Solution (Immutable Strings)
In this compliant solution, the characters referred to by the pointer c
are const
-qualified, meaning that any attempt to assign them to different values is an error:
Code Block | ||||
---|---|---|---|---|
| ||||
wchar_t const *c = L"Hello";
|
Compliant Solution (Mutable Strings)
In cases where the string is meant to be modified, use initialization instead of assignment. In this compliant solution, c
is a modifiable wchar_t
array that has been initialized using the contents of the corresponding string literal:
Code Block | ||||
---|---|---|---|---|
| ||||
wchar_t c[] = L"Hello";
|
Consequently, a statement such as c[0] = L'C'
is valid and behaves as expected.
...
Risk Assessment
Modifying string literals causes undefined behavior, resulting in abnormal program termination and denial-of-service vulnerabilities.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
STR05 |
1 (low)
3 (likely)
2(medium)
P6
L2
References:
-C | Low | Unlikely | Low | P3 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Astrée |
| literal-assignment | Fully checked | ||||||
Axivion Bauhaus Suite |
| CertC-STR05 | |||||||
Clang |
| -Wwrite-strings | Not enabled by -Weverything | ||||||
CodeSonar |
| LANG.TYPE.NCS | Non-const string literal | ||||||
Compass/ROSE | |||||||||
| CC2.STR05 | Fully implemented | |||||||
GCC |
| -Wwrite-strings | |||||||
Helix QAC |
| C0752, C0753 | |||||||
Klocwork |
| MISRA.STRING_LITERAL.NON_CONST.2012 | |||||||
LDRA tool suite |
| 623 S | Fully implemented | ||||||
Parasoft C/C++test |
| CERT_C-STR05-a | A string literal shall not be modified | ||||||
PC-lint Plus |
| 1776 | Fully supported | ||||||
RuleChecker |
| literal-assignment | Fully checked |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Bibliography
[Corfield 1993] | |
[Lockheed Martin 2005] | AV Rule 151.1 |
...
[http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1993/N0389.asc]
\[[ISO/IEC 9899-1999:TC2|AA. C References#ISO/IEC 9899-1999TC2]\] Section 6.7.8, "Initialization"
\[Lockheed Martin 2005\] Lockheed Martin. Joint Strike Fighter Air Vehicle C+\+ Coding Standards for the System Development and Demonstration Program. Document Number 2RDU00001, Rev C. December 2005. AV Rule 151.1 Wiki Markup