...
Note that this recommendation does not apply to character arrays initialized with string literals, see STR36-C. Do not specify the bound of a character array initialized with a string literal for more information.
...
Noncompliant Code Example (Incorrect Size)
This non-compliant noncompliant code example initializes an array of integers using an initialization with too many elements for the array.
...
The size of the array a
is three, although the size of the initialization is four. The last element of the initialization (4
) is ignored. Most compilers will diagnose this error.
Implementation Details
This non-compliant noncompliant code example generates a warning in gcc
. Microsoft Visual Studio 2008 generates a fatal diagnostic: error C2078: too many initializers
.
...
Noncompliant Code Example (Implicit Size)
In this example, the compiler allocates an array of four integer elements, and because an array bound is not explicitly specified by the programmer, sets the array bound to 4
. However, if the initializer changes, the array bound may also change, causing unexpected results.
Code Block | ||
---|---|---|
| ||
int a[] = {1, 2, 3, 4}; |
Compliant Solution
This compliant solution explicitly specifies the array bound.
...
Explicitly specifying the array bound although it is implicitly defined by an initializer allows a compiler or other static analysis tool to issue a diagnostic if these values do not agree.
Risk Assessment
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
ARR02-A C | medium | unlikely | low | P6 | L2 |
Automated Detection
Compass/ROSE can detect violations of this recommendation
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
Wiki Markup |
---|
\[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.7.8, "Initialization" |
...