Main Content

CERT C: Rec. DCL13-C

Declare function parameters that are pointers to values not changed by the function as const

Description

Rule Definition

Declare function parameters that are pointers to values not changed by the function as const.1

Polyspace Implementation

The rule checker checks for Pointer to non-cont qualified function parameter.

Examples

expand all

Issue

The rule checker flags a pointer to a non-const function parameter if the pointer does not modify the addressed object. The assumption is that the pointer is not meant to modify the object and so must point to a const-qualified type.

Risk

This rule ensures that you do not inadvertently use pointers to modify objects.

Example - Pointer That Should Point to const-Qualified Types
#include <string.h>

typedef unsigned short uint16_t;

uint16_t ptr_ex(uint16_t *p) {      /* Non-compliant */
    return *p;
}

char last_char(char * const s){     /* Non-compliant */
    return s[strlen(s) - 1u];
}

uint16_t first(uint16_t a[5]){      /* Non-compliant */
    return a[0];
}

This example shows three different noncompliant pointer parameters.

  • In the ptr_ex function, p does not modify an object. However, the type to which p points is not const-qualified, so it is noncompliant.

  • In last_char, the pointer s is const-qualified but the type it points to is not. This parameter is noncompliant because s does not modify an object.

  • The function first does not modify the elements of the array a. However, the element type is not const-qualified, so a is also noncompliant.

Correction — Use const Keywords

One possible correction is to add const qualifiers to the definitions.

#include <string.h>

typedef unsigned short uint16_t;

uint16_t ptr_ex(const uint16_t *p){     /* Compliant */
    return *p;
}

char last_char(const char * const s){   /* Compliant */
    return s[strlen( s ) - 1u];
}

uint16_t first(const uint16_t a[5]) {   /* Compliant */
    return a[0];
}

Check Information

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

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.