Main Content

CERT C: Rec. DCL05-C

Use typedefs of non-pointer types only

Since R2026a

Description

Use typedefs of non-pointer types only1

Polyspace Implementation

The rule checker checks for Use of typedef of pointer types.

Examples

expand all

Issue

This issue occurs when the code defines a typedef of a pointer type that points to a non-const and non-volatile type and then uses the typedef with a const or volatile qualifier. For example:

typedef int* intptr;
const intptr p2i; //Noncompliant

Risk

If you apply const or volatile qualifier to a typedef of a pointer type, the qualifier is applied to the pointer type, not the underlying object to which the pointer points. For example, in this code, p2i is a const pointer to a non-const integer. It is possible to modify the underlying object, which can be unexpected.

void foo(const intptr p2i){
    *(p2i)++; //Modify the content to p2i
    // ++p2i; Fails compilation  
}
Using typedef for pointer types can result in code that is not const-correct.

Fix

To fix this issue:

  • Define typedefs for base nonpointer types.

  • For typedefs of pointer types, include the const-correct definition of the type in the typedef statement.

Example

In this code, Polyspace® reports violation on the use of const-qualifier on the typedef of a pointer type.

typedef struct {
    int x;
    int y;
} *PointPtr;

float getDistance(const PointPtr firstPoint, const PointPtr secondPoint){ //Noncompliant 
    //
}

Correction

To fix this issue, change the typedef definition. You can take either of these steps:

  • Change the typedef to a nonpointer type and then use a pointer of the typedef with appropriate const-ness. This is demonstrated by the function getDistance().

  • Include the appropriate const qualifier directly in the typedef. This is demonstrated by the function alternateGetDistance().

typedef struct {
    int x;
    int y;
} Point;

float getDistance(const Point *firstPoint, const Point *secondPoint){ //Compliant
    //
}


typedef const struct {
    int x;
    int y;
} *Ptr2ConstPoint;


float alternateGetDistance(Ptr2ConstPoint firstPoint, Ptr2ConstPoint SecondPoint){ //Compliant
    //...
}

Check Information

Group: Rec. 02. Declarations and Initialization (DCL)
PQL Name: std.cert.DCL05_C

Version History

Introduced in R2026a


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.