Versions Compared

Key

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

...

A better approach is to not specify the bound of a string initialized with a string literal because the compiler will automatically allocate sufficient space for the entire string literal, including the terminating null character. This rule is a specific exception to ARR02-C. Explicitly specify array bounds, even if implicitly defined by an initializer.

Noncompliant Code Example

...

This approach is preferred because the size of the array can always be derived even if the size of the string literal changes.

Exceptions

STR11-C-EX1: If the intention is to create a character array and not a null-terminated byte string, initializing to fit exactly without a null byte is allowed but not recommended. The preferred approach to create an array containing just the three characters 'a', 'b', and 'c', for example, is to declare each character literal as a separate element as follows:

...

Also, you should make clear in comments or documentation if a character array is, in fact, not a null-terminated byte string.

STR11-C-EX2: If the character array must be larger than the string literal it is initialized with, you may explicitly specify an array bounds. This is particularly important if the array's contents might change during program execution.

Code Block
bgColor#ccccff
langc
#include <string.h>
 
void func(void) {
  char s[10] = "abc";
  strcpy(&s[3], "def");
}

Risk Assessment

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

STR11-C

Low

Probable

Low

P6

L2

Automated Detection

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V

Supported: Astrée can detect subsequent code defects that this rule aims to prevent.
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-STR11
Compass/ROSE

 

 

 




ECLAIR

Include Page
ECLAIR_V
ECLAIR_V

CC2.STR36

Fully implemented

Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C1312
LDRA tool suite
Include Page
LDRA_V
LDRA_V
404 SPartially implemented
PRQA QA-C Include PagePRQA QA-C_vPRQA QA-C_v1312,0690
Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V

CERT_C-STR11-a

Do not specify the bound of a character array initialized with a string literal

PC-lint Plus

Include Page
PC-lint Plus_V
PC-lint Plus_V

784

Partially supported

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rec. STR11-C

Checks for missing null in string array (rec. partially covered)

Partially implemented

Splint
Include Page
Splint_V
Splint_V

 

 



Related Vulnerabilities

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

Related Guidelines

Bibliography

[ECTC 1998]Section A.8, "Character Array Initialization"
[ISO/IEC 9899:2011]Subclause 6.7.9, "Initialization"
[Seacord 2013]Chapter 2, "Strings"

...


...

Image Modified Image Modified Image Modified