Main Content

CERT C: Rec. DCL00-C

Const-qualify immutable objects

Since R2020b

Description

Rule Definition

Const-qualify immutable objects.1

Polyspace Implementation

The rule checker checks for Unmodified variable not const-qualified.

Examples

expand all

Issue

Unmodified variable not const-qualified occurs when a local variable is not const-qualified and one of the following statements is true during the variable lifetime:

  • You do not perform write operations on the variable after initialization.

  • When you perform write operations, you reassign the same constant value to the variable.

The checker considers a variable as modified if its address is assigned to a pointer or reference (unless it is a pointer or reference to a const variable), passed to another function, or otherwise used. In these situations, the checker does not suggest adding a const qualifier.

The checker flags arrays as candidates for const-qualification only if you do not perform write operations on the array elements at all after initialization. The checker does not flag function parameters of integer, float, enum, or boolean types.

Risk

const-qualifying a variable avoids unintended modification of the variable during later code maintenance. The const qualifier also indicates to a developer that the variable retains its initial value in the remainder of the code.

Fix

If you do not expect to modify a variable value during its lifetime, add the const qualifier to the variable declaration and initialize the variable at declaration.

If you expect the variable to be modified, see if the absence of a modification indicates a programming omission and fix the issue.

Example - Missing const Qualification on Pointer
#include <string.h>

char returnNthCharacter (int n) {
    char* pwd = "aXeWdf10fg" ; //Noncompliant
    char nthCharacter;
        
    for(int i=0; i < strlen(pwd); i++) {
        if(i==n)
            nthCharacter = pwd[i];
    }
    return nthCharacter;
}

In this example, the pointer pwd is not const-qualified. However, beyond initialization with a constant, it is not reassigned anywhere in the returnNthCharacter function.

Correction – Add const at Variable Declaration

If the variable is not intended to be modified, add the const qualifier at declaration. In this example, both the pointer and the pointed variable are not modified. Add a const qualifier to both the pointer and the pointed variable. Later modifications cannot reassign the pointer pwd to point at a different variable nor modify the value at the pointed location.

#include <string.h>

char returnNthCharacter (int n) {
    const char* const pwd = "aXeWdf10fg" ; //Compliant
    char nthCharacter;
        
    for(int i=0; i < strlen(pwd); i++) {
        if(i==n)
            nthCharacter = pwd[i];
    }
    return nthCharacter;
}

Note that the checker only flags the missing const from the pointer declaration. The checker does not determine if the pointed location also merits a const qualifier.

Check Information

Group: Rec. 02. Declarations and Initialization (DCL)

Version History

Introduced in R2020b

expand all


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.