Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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.35,  paragraph paragraph 11 [ISO/IEC 9899:20112024]:

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.

...

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

Noncompliant Code Example

...

You use a preprocessor directive in the argument PRQA QACv1072 

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V
macro-argument-hashFully checked
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-PRE32Fully implemented
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V
LANG.PREPROC.MACROARGPreprocessing directives in macro argument

ECLAIR

Include Page
ECLAIR_V
ECLAIR_V

CC2.PRE32

Fully implemented

Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C0853

C++1072

Fully implemented
Klocwork

Include Page
Klocwork_V
Klocwork_V

MISRA.EXPANSION.DIRECTIVEFully implemented
LDRA tool suite
Include Page
LDRA_V
LDRA_V
341 SFully implemented
Parasoft C/C++test

Include Page
Parasoft_V
Parasoft_V

MISRA2004-19_9Implemented

Polyspace Bug Finder

R2018aPreprocessor directive in macro argumentCERT_C-PRE32-a
Arguments to a function-like macro shall not contain tokens that look like preprocessing directives
PC-lint PlusPRQA QA-C

Include Page

PC-

lint Plus_

V

PRQA QA-C_v853PRQA QA-C++

PC-lint Plus_V

436, 9501

Fully supported

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rule PRE32-CChecks for preprocessor directive in macro argument (rule fully covered)
RuleChecker
Include Page
RuleChecker_V
RuleChecker_V
macro-argument-hashFully checked

Related Vulnerabilities

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

...

[GCC Bugs]"Non-bugs"
[ISO/IEC 9899:20112024]6.10.35, "Macro Replacement"


...