The C standard allows an array variable to be declared both with a bound index and with an initialization literal. The initialization literal also implies an array size , in the number of elements specified. For strings, the size specified by a string literal is the number of characters in the literal plus one for the terminating null character.
It is common for an array variable to be initialized by a string literal and declared with an explicit bound that matches the number of characters in the string literal. C99, Section 6.7.8, "Initialization", paragraph 14, says9, para. 14 [ISO/IEC 9899:2011], says:
An array of character type may be initialized by a character string literal or UTF−8 string literal, optionally enclosed in braces. Successive characters bytes of the character string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.
However, if the string is intended to be used as a null-terminated byte string, then the array will have one too few characters to hold the string because it does not account for the terminating null character. Such a sequence of characters has limited utility and has the potential to cause vulnerabilities if a null-terminated byte string is assumed.
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 recommendation ARR02-C. Explicitly specify array bounds, even if implicitly defined by an initializer.
...
Code Block | ||||
---|---|---|---|---|
| ||||
const char s[3] = "abc";
|
The size of the array s
is three3, although the size of the string literal is four4. Any subsequent use of the array as a null-terminated byte string can result in a vulnerability, because s
is not properly null-terminated. (See rule STR32-C. Null-terminate byte strings as required.)
...
This code produces a compilation error in MSVC 2008. This code It compiles with no warning in GCC 4.3.3. It produces a 3-character array with no terminating null character, as specified by the standard.
...
This compliant solution does not specify the bound of a character array in the array declaration. If the array bound is omitted, the compiler will allocate allocates sufficient storage to store the entire string literal, including the terminating null character.
Code Block | ||||
---|---|---|---|---|
| ||||
const char s[] = "abc";
|
This is the approach is preferred approach because the size of the array can always be derived even if the size of the string literal changes.
...
STR36-EX0: 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:
Code Block | ||||
---|---|---|---|---|
| ||||
char s[3] = { 'a', 'b', 'c' }; /* NOT a string */
|
Also, you should make clear in comments or documentation if a character array is, in fact, not a null-terminated byte string.
STR36-EX1: If the char 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 | ||||
---|---|---|---|---|
| ||||
char s[10] = "abc";
strcpy(&s[3], "def");
|
...
Tool | Version | Checker | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Section | Splint |
|
|
| |||||||
Section | Compass/ROSE |
|
|
| |||||||
Section | |
| Section | araydecl section | Fully Implementedimplemented |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...
CERT C++ Secure Coding Standard: STR36-CPP. Do not specify the bound of a character array initialized with a string literal
ISO/IEC 9899:19992011 Section 6.7.89, "Initialization"
ISO/IEC TR 24772 "CJM String Terminationtermination"
Bibliography
[ECTC 1998] A.8, "Character array initialization"
[Seacord 2005a] Chapter 2, "Strings"
...