Main Content

CERT C: Rule DCL37-C

Do not declare or define a reserved identifier

Description

Rule Definition

Do not declare or define a reserved identifier.1

Polyspace Implementation

The rule checker checks for these issues:

  • Defining and undefining reserved identifiers or macros.

  • Declaring a reserved identifier or macro name.

If your code declares or defines a reserved identifier:

  • If there is an explicit definition, Polyspace® flags it.

  • If there is no explicit definition, the compiler makes an implicit definition when your code uses the symbol. Polyspace flags such a use.

This violation is reported once for each noncompliant identifier.

Examples

expand all

Issue

The issue occurs when you use #define and #undef on a reserved identifier or reserved macro name.

Risk

Reserved identifiers and reserved macro names are intended for use by the implementation. Removing or changing the meaning of a reserved macro can result in undefined behavior. This rule applies to the following:

  • Identifiers or macro names beginning with an underscore

  • Identifiers in file scope described in the C Standard Library (ISO®/IEC 9899:1999, Section 7, "Library")

  • Macro names described in the C Standard Library as being defined in a standard header (ISO/IEC 9899:1999, Section 7, "Library").

Example - Defining or Undefining Reserved Identifiers
#undef __LINE__               /* Non-compliant - begins with _ */
#define _Guard_H 1            /* Non-compliant - begins with _ */
#undef _ BUILTIN_sqrt        /* Non-compliant - implementation may
                               * use _BUILTIN_sqrt for other purposes,
                               * e.g. generating a sqrt instruction */
#define defined               /* Non-compliant - reserved identifier */
#define errno my_errno        /* Non-compliant - library identifier */
#define isneg(x) ( (x) < 0 )  /* Compliant - rule doesn't include  
                               * future library directions   */
Issue

The issue occurs when you declare a reserved identifier or macro name.

If you define a macro name that corresponds to a standard library macro, object, or function, Polyspace considers this a violation of the rule.

The rule considers tentative definitions as definitions.

Risk

The Standard allows implementations to treat reserved identifiers specially. If you reuse reserved identifiers, you can cause undefined behavior.

Check Information

Group: Rule 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.