Main Content

CERT C: Rec. PRE11-C

Do not conclude macro definitions with a semicolon

Since R2020a

Description

Rule Definition

Do not conclude macro definitions with a semicolon.1

Polyspace Implementation

The rule checker checks for Macro terminated with a semicolon.

Examples

expand all

Issue

Macro terminated with a semicolon occurs when a macro that is invoked at least once has a definition ending with a semicolon.

Risk

If a macro definition ends with a semicolon, the macro expansion can lead to unintended program logic in certain contexts, such as within an expression.

For instance, consider the macro:

#define INC_BY_ONE(x) ++x;
If used in the expression:
res = INC_BY_ONE(x)%2;
the expression resolves to:
res = ++x; %2;
The value of x+1 is assigned to res, which is probably unintended. The leftover standalone statement %2; is valid C code and can only be detected by enabling strict compiler warnings.

Fix

Do not end macro definitions with a semicolon. Leave it up to users of the macro to add a semicolon after the macro when needed.

Alternatively, use inline functions in preference to function-like macros that involve statements ending with semicolon.

Example – Spurious Semicolon in Macro Definition
#define WHILE_LOOP(n) while(n>0); //Noncompliant

void performAction(int timeStep);

void main() {
    int loopIter = 100;
    WHILE_LOOP(loopIter) {
        performAction(loopIter);
        loopIter--;
    }
}

In this example, the defect occurs because the definition of the macro WHILE_LOOP(n) ends with a semicolon. As a result of the semicolon, the while loop has an empty body and the following statements run only once. It was probably intended that the loop must run 100 times.

Correction – Remove Semicolon from Macro Definition

Remove the trailing semicolon from the macro definition. Users of the macro can add a semicolon after the macro when needed. In this example, a semicolon is not required.

#define WHILE_LOOP(n) while(n>0)

void performAction(int timeStep);

void main() {
    int loopIter = 100;
    WHILE_LOOP(loopIter) {
        performAction(loopIter);
        loopIter--;
    }
}

Check Information

Group: Rec. 01. Preprocessor (PRE)

Version History

Introduced in R2020a


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.