Main Content

CERT C: Rule PRE32-C

Do not use preprocessor directives in invocations of function-like macros

Description

Rule Definition

Do not use preprocessor directives in invocations of function-like macros.1

Polyspace Implementation

The rule checker checks for Preprocessor directive in macro argument.

Examples

expand all

Issue

Preprocessor directive in macro argument occurs when you use a preprocessor directive in the argument to a function-like macro or a function that might be implemented as a function-like macro.

For instance, a #ifdef statement occurs in the argument to the function like macro foo():

#define foo(X) //...    
foo(
    #ifdef A
    "A"  
    #else
    "B"
    #endif
  );
The checker reports a violation on this kind of usage of #ifdef statements. Violations are also reported when #ifdef statements are used as arguments of library functions that might be implemented as macros. Examples of library functions that might be implemented as macros include assert(), memcpy(), and printf().

Risk

During preprocessing, a function-like macro call is replaced by the macro body and the parameters are replaced by the arguments to the macro call (argument substitution). Suppose a macro min() is defined as follows.

#define min(X, Y)  ((X) < (Y) ? (X) : (Y))
When you call min(1,2), it is replaced by the body ((X) < (Y) ? (X) : (Y)). X and Y are replaced by 1 and 2.

According to the C11 Standard (Sec. 6.10.3), if the list of arguments to a function-like macro itself has preprocessing directives, the argument substitution during preprocessing is undefined.

Fix

To ensure that the argument substitution happens in an unambiguous manner, use the preprocessor directives outside the function-like macro.

For instance, to execute foo with different arguments based on a #ifdef directive, call foo multiple times within the #ifdef directive branches.

#ifdef A
    foo("A");
#else
    foo("B")
#endif

Example - Directives in Function-Like Macros

In this example, the preprocessor directives #ifdef and #endif occur in the argument to the function-like macro print(). Polyspace® reports violations on the preprocessor directives in macro arguments.

#include <stdio.h>

#define print(A) printf(#A)

void func(void) {
    print(
#ifdef SW //Noncompliant
          "Message 1"
#else
          "Message 2"
#endif //Noncompliant
         );
}
Correction — Use Directives Outside Macro

One possible correction is to use the function-like macro multiple times in the branches of the #ifdef directive.

#include <stdio.h>

#define print(A) printf(#A)

void func(void) {
#ifdef SW
        print("Message 1");
#else  
        print("Message 2");
#endif 
}

Check Information

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