Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

According to the C Standard, 6.4.5, paragraph 3 [ISO/IEC 9899:20112024]:

A character string literal is a sequence of zero or more multibyte characters enclosed in double-quotes, as in "xyz". A UTF−8 UTF-8 string literal is the same, except prefixed by u8. A wide wchar_t string literal is the same, except prefixed by the letter L, u, or U.L. A UTF-16 string literal is the same, except prefixed by u. A UTF-32 string literal is the same, except prefixed by U. Collectively, wchar_t, UTF-16, and UTF-32 string literals are called wide string literals.

At compile time, string literals are used to create an array of static storage duration of sufficient length to contain the character sequence and a terminating null character. String literals are usually referred to by a pointer to (or array of) characters. Ideally, they should be assigned only to pointers to (or arrays of) const char or const wchar_t. It is unspecified whether these arrays of string literals are distinct from each other. The behavior is undefined if a program attempts to modify any portion of a string literal. Modifying a string literal frequently results in an access violation because string literals are typically stored in read-only memory. (See undefined behavior 3332.)

Avoid assigning a string literal to a pointer to non-const or casting a string literal to a pointer to non-const. For the purposes of this rule, a pointer to (or array of) const characters must be treated as a string literal. Similarly, the returned value of the following library functions must be treated as a string literal if the first argument is a string literal:

...

In this noncompliant code example, the char pointer p is str is initialized to the address of a string literal. Attempting to modify the string literal is undefined behavior 32:

Code Block
bgColor#FFcccc
langc
char *pstr  = "string literal";
pstr[0] = 'S';

Compliant Solution

As an array initializer, a string literal specifies the initial values of characters in an array as well as the size of the array. (See STR11-C. Do not specify the bound of a character array initialized with a string literal.) This code creates a copy of the string literal in the space allocated to the character array a str. The string stored in a can str can be modified safely.

Code Block
bgColor#ccccff
langc
char astr[] = "string literal";
astr[0] = 'S';

Noncompliant Code Example (POSIX)

...

Modifying string literals can lead to abnormal program termination and possibly denial-of-service attacks.

Rule

Severity

Likelihood

Detectable

Remediation CostRepairable

Priority

Level

STR30-C

Low

Likely

No

YesLow

P9P6

L2

Automated Detection


_VSupported, but no explicit checkerPB-27Writing

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée
_V
string-literal-modfication
write-to-string-literal
Fully checked
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-STR30Fully implemented
Compass/ROSE

Can detect simple violations of this rule

Coverity
Include Page
Coverity_V
Coverity_V
PWDeprecates conversion from a string literal to "char *"
Cppcheck Premium 

Include Page
Cppcheck Premium_V
Cppcheck Premium_V

premium-cert-str30-c 
Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C0556, C0752, C0753, C0754

C++3063, C++3064, C++3605, C++3606, C++3607


Klocwork

Include Page
Klocwork_V
Klocwork_V

CERT.STR.ARG.CONST_TO_NONCONST
CERT.STR.ASSIGN.CONST_TO_NONCONST


LDRA tool suite
Include Page
LDRA_V
LDRA_V

157 S

Partially implemented
Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V

CERT_C-STR30-a
CERT_C-STR30-b

A string literal shall not be modified
Do not modify string literals

PC-lint Plus

Include Page
PC-lint Plus_V
PC-lint Plus_V

489, 1776

Partially supported

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V

Polyspace Bug Finder

R2016a

_V

CERT C: Rule STR30-CChecks for writing to const qualified object

Object declared with a const qualifier is modified

(rule fully covered)


PVS-StudioPRQA QA-C

Include Page

PRQA QA

PVS-

C

Studio_

v

V

PRQA QA

PVS-

C_v

0556
0752
0753

Studio_V

V675
RuleChecker

Include Page
RuleChecker_V
RuleChecker_V

string-literal-modficationPartially checkedPartially implemented
Splint

Include Page
Splint_V
Splint_V

PRQA QA-C++4.2 3063, 3064, 3605, 3606, 3607, 3842 
PVS-Studio6.22V675General analysis rule


TrustInSoft Analyzer

Include Page
TrustInSoft Analyzer_V
TrustInSoft Analyzer_V

mem_accessExhaustively verified (see one compliant and one non-compliant example).

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

...

Bibliography

[ISO/IEC 9899:20112024]6.4.5, "String Literals"
[Plum 1991]Topic 1.26, "Strings—String Literals"
[Summit 1995]comp.lang.c FAQ List, Question 1.32

...