Skip to content

Latest commit

 

History

History
89 lines (55 loc) · 5.68 KB

File metadata and controls

89 lines (55 loc) · 5.68 KB

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

This query implements the CERT-C rule PRE32-C:

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

Description

The arguments to a macro must not include preprocessor directives, such as #define, #ifdef, and #include. Doing so results in undefined behavior, according to the C Standard, 6.10.3, paragraph 11 [ISO/IEC 9899:2011]:

The sequence of preprocessing tokens bounded by the outside-most matching parentheses forms the list of arguments for the function-like macro. The individual arguments within the list are separated by comma preprocessing tokens, but comma preprocessing tokens between matching inner parentheses do not separate arguments. If there are sequences of preprocessing tokens within the list of arguments that would otherwise act as preprocessing directives, the behavior is undefined.

See also undefined behavior 93.

This rule also applies to the use of preprocessor directives in arguments to any function where it is unknown whether or not the function is implemented using a macro. This includes all standard library functions, such as memcpy(), printf(), and assert(), because any standard library function may be implemented as a macro. (C11, 7.1.4, paragraph 1).

Noncompliant Code Example

In this noncompliant code example [GCC Bugs], the programmer uses preprocessor directives to specify platform-specific arguments to memcpy(). However, if memcpy() is implemented using a macro, the code results in undefined behavior.

#include <string.h>
 
void func(const char *src) {
  /* Validate the source string; calculate size */
  char *dest;
  /* malloc() destination string */ 
  memcpy(dest, src,
    #ifdef PLATFORM1
      12
    #else
      24
    #endif
  );
  /* ... */
}

Compliant Solution

In this compliant solution [GCC Bugs], the appropriate call to memcpy() is determined outside the function call:

#include <string.h>

void func(const char *src) {
  /* Validate the source string; calculate size */
  char *dest;
  /* malloc() destination string */ 
  #ifdef PLATFORM1
    memcpy(dest, src, 12);
  #else
    memcpy(dest, src, 24);
  #endif
  /* ... */
}

Risk Assessment

Including preprocessor directives in macro arguments is undefined behavior.

Rule Severity Likelihood Remediation Cost Priority Level
PRE32-C Low Unlikely Medium P2 L3

Automated Detection

Tool Version Checker Description
Astrée 22.04 macro-argument-hash Fully checked
Axivion Bauhaus Suite 7.2.0 CertC-PRE32 Fully implemented
CodeSonar 7.0p0 LANG.PREPROC.MACROARG Preprocessing directives in macro argument
ECLAIR 1.2 CC2.PRE32 Fully implemented
Helix QAC 2022.2 C0853 C++1072
Klocwork 2022.2 MISRA.EXPANSION.DIRECTIVE
LDRA tool suite 9.7.1 341 S Fully implemented
Parasoft C/C++test 2022.1 CERT_C-PRE32-a Arguments to a function-like macro shall not contain tokens that look like preprocessing directives
PC-lint Plus 1.4 436, 9501 Fully supported
Polyspace Bug Finder R2022a CERT C: Rule PRE32-C Checks for preprocessor directive in macro argument (rule fully covered)
PRQA QA-C 9.7 0853
PRQA QA-C++ 4.4 1072
RuleChecker 22.04 macro-argument-hash Fully checked

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Bibliography

\[ GCC Bugs \] "Non-bugs"
\[ ISO/IEC 9899:2011 \] 6.10.3, "Macro Replacement"

Implementation notes

This query defines end of function call as the next node in the control flow graph.

References