Versions Compared

Key

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

...

Include Page
c:STR30 NCCE 1
c:STR30 NCCE 1

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). This code creates a copy of the string literal in the space allocated to the character array a. The string stored in a can be safely modified.

Code Block

char a[] = "string literal";
a[0] = 'S';

Non-Compliant Code Example

In this non-compliant example, the mktemp() function modifies its string argument.

Code Block

mktemp("/tmp/edXXXXXX");

Compliant Solution

Instead of passing a string literal, use a named array:

...

(include:STR30 CS 1)

Include Page
c:STR30 NCCE 2
c:STR30 NCCE 2

(include:STR30 CS 2)

Priority: P9 Level: L1

Modifying string literals can lead to abnormal program termination and results in undefined behavior that can be used in denial-of-service attacks.

...