Main Content

CERT C++: 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 a memcpy function. The memcpy function might be implemented as a macro.

memcpy(dest, src,
    #ifdef PLATFORM1
      12
    #else
      24
    #endif
  );
The checker flags similar usage in printf and assert, which can also be implemented as macros.

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 memcpy with different arguments based on a #ifdef directive, call memcpy multiple times within the #ifdef directive branches.

#ifdef PLATFORM1
    memcpy(dest, src, 12);
#else
    memcpy(dest, src, 24);
#endif

Example - Directives in Function-Like Macros
#include <stdio.h>

#define print(A) printf(#A)

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

In this example, the preprocessor directives #ifdef and #endif occur in the argument to the function-like macro print().

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: 49. Miscellaneous (MSC)

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.