Main Content

CERT C: Rec. ARR02-C

Explicitly specify array bounds, even if implicitly defined by an initializer

Description

Rule Definition

Explicitly specify array bounds, even if implicitly defined by an initializer.1

Polyspace Implementation

The rule checker checks for the issue Improper array initialization.

Examples

expand all

Issue

Improper array initialization 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.

IssueRiskPossible 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.

Fix

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:

Example - Incorrectly Placed Braces (C Only)

int arr[2][3]
= {{1, 2},
    {3, 4},
    {5, 6} //Noncompliant
};

In this example, the array arr is initialized as {1,2,0,3,4,0}. Because the initializer contains {5,6}, you might expect the array to be initialized {1,2,3,4,5,6}.

Correction — Place Braces Correctly

One possible correction is to place the braces correctly so that all elements are explicitly initialized.

int a1[2][3]
= {{1, 2, 3},
    {4, 5, 6}
};
Example - First Element Not Explicitly Initialized
int arr[5]
= {
    [1] = 2,      
    [2] = 3,
    [3] = 4,
    [4] = 5
};               //Noncompliant

In this example, arr[0] is not explicitly initialized. It is possible that the programmer did not consider that the array indexing starts from 0.

Correction — Explicitly Initialize All Elements

One possible correction is to initialize all elements explicitly.

int arr[5]
= {
    [0] = 1,
    [1] = 2,      
    [2] = 3,
    [3] = 4,
    [4] = 5
};              
Example - Element Initialized Twice
int arr[5]
= {
    [0] = 1,
    [1] = 2,      
    [2] = 3,
    [2] = 4, //Noncompliant
    [4] = 5
};              

In this example, arr[2] is initialized twice. The first initialization is overridden. In this case, because arr[3] was not explicitly initialized, it is possible that the programmer intended to initialize arr[3] when arr[2] was initialized a second time.

Correction — Fix Redundant Initialization

One possible correction is to eliminate the redundant initialization.

int arr[5]
= {
    [0] = 1,
    [1] = 2,
    [2] = 3,
    [3] = 4,
    [4] = 5
};
Example - Mix of Designated and Nondesignated Initializers
int arr[]
= {
    [0] = 1,
    [3] = 3,
    4,
    [5] = 5,
    6
    };               //Noncompliant

In this example, because a mix of designated and nondesignated initializers are used, it is difficult to determine the size of arr by inspection.

Correction — Use Only Designated Initializers

One possible correction is to use only designated initializers for array initialization and to specify the size of the array explicitly.

int arr[7]
= {
    [0] = 1,
    [3] = 3,
    [4] = 4,
    [5] = 5,
    [6] = 6
};              

Check Information

Group: Rec. 06. Arrays (ARR)

Version History

Introduced in R2019a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.