Main Content

CERT C: Rec. PRE01-C

Use parentheses within macros around parameter names

Description

Rule Definition

Use parentheses within macros around parameter names.1

Polyspace Implementation

The rule checker checks for Macro parameters not enclosed in parentheses.

Examples

expand all

Issue

The issue occurs when a macro parameter contains an expression but you do not enclose the parameter in parentheses either in the macro definition or in the macro use.

If a macro parameter does not contain an expression, then the parentheses are not necessary.

Risk

If you do not enclose macro parameters containing expressions in parentheses, when parameter substitution occurs, operator precedence might not give the results that you want.

Fix

If a macro parameter contains an expression, enclose the parameter in parenthesis in the macro definition or macro use.

Example - Macro Expressions
#define mac1(x, y) (x * y)
#define mac2(x, y) ((x) * (y))

void foo(void){
    int r;

    r = mac1(1 + 2, 3 + 4);       /* Non-compliant */
    r = mac1((1 + 2), (3 + 4));   /* Compliant */

    r = mac2(1 + 2, 3 + 4);       /* Compliant */
}

In this example, mac1 and mac2 are macros that presumably implement the same definition.

  • The definition of mac1 does not enclose the macro parameters in parentheses. In the noncompliant expression, the macro expands to r = (1 + 2 * 3 + 4); The intended expression can be (1 + (2 * 3) + 4) or (1 + 2) * (3 + 4). However, without parentheses, a developer or code reviewer might not know the intent of the expression. The subsequent compliant expression encloses the macro parameters in parentheses, so the intended macro expansion is clearly (1 + 2) * (3 + 4).

  • The definition of mac2 encloses the parameters in parentheses. The expression involving mac2 expands to (1 + 2) * (3 + 4) and complies with the rule.

Check Information

Group: Rec. 01. Preprocessor (PRE)

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.