Incorrect array initialization when using initializers
This defect occurs when Polyspace® Bug Finder™ considers that an array initialization using initializers is incorrect.
This defect applies to normal and designated initializers. In
C99, with designated initializers, you can place the elements of an
array initializer in any order and implicitly
initialize some array elements. The designated initializers use the
array index to establish correspondence between an array element and
an array initializer element. For instance, the statement int
arr[6] = { [4] = 29, [2] = 15 } is equivalent to int
arr[6] = { 0, 0, 15, 0, 29, 0 }.
You can use initializers incorrectly in one of the following ways.
| Issue | Risk | Possible Fix |
|---|---|---|
In your initializer for a one-dimensional array, you have more elements than the array size. | Unused array initializer elements indicate a possible coding error. | Increase the array size or remove excess elements. |
You place the braces enclosing initializer values incorrectly. | Because of the incorrect placement of braces, some array initializer elements are not used. Unused array initializer elements indicate a possible coding error. | Place braces correctly. |
In your designated initializer, you do not initialize the first element of the array explicitly. | The implicit initialization of the first array element indicates a possible coding error. You possibly overlooked the fact that array indexing starts from 0. | Initialize all elements explicitly. |
In your designated initializer, you initialize an element twice. | The first initialization is overridden. The redundant first initialization indicates a possible coding error. | Remove the redundant initialization. |
You use designated and nondesignated initializers in the same initialization. | You or another reviewer of your code cannot determine the size of the array by inspection. | Use either designated or nondesignated initializers. |
The fix depends on the root cause of the defect. Often the result details show a sequence of events that led to the defect. You can implement the fix on any event in the sequence. If the result details do not show the event history, you can trace back using right-click options in the source code and see previous related events. See also Interpret Bug Finder Results in Polyspace Desktop User Interface.
See examples of fixes below.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See Address Polyspace Results Through Bug Fixes or Justifications.
| Group: Programming |
| Language: C | C++ |
| Default: On for handwritten code, off for generated code |
Command-Line Syntax: IMPROPER_ARRAY_INIT |
| Impact: Medium |
| CWE ID: 665 |