Macros are frequently used to make source code more readable. Macro definitions, regardless of whether they expand to a single or multiple statements, should not conclude with a semicolon. (See PRE10-C. Wrap multistatement macros in a do-while loop.) If required, the semicolon should be included following the macro expansion. Inadvertently inserting a semicolon at the end of the macro definition can unexpectedly change the control flow of the program.
Another way to avoid this problem is to prefer inline or static functions over function-like macros. (See also PRE00-C. Prefer inline or static functions to function-like macros.)
In general, the programmer should ensure that there is no semicolon at the end of a macro definition. The responsibility for having a semicolon where needed during the use of such a macro should be delegated to the person invoking the macro.
Noncompliant Code Example
This noncompliant code example creates a macro definition for a for
loop in the program. A for
loop should require braces, even if it contains only a single body statement. (See EXP19-C. Use braces for the body of an if, for, or while statement.) This macro takes an integer argument, which is the number of times the loop should run. The programmer has inserted a semicolon at the end of the macro definition by mistake.
#define FOR_LOOP(n) for(i=0; i<(n); i++); int i; FOR_LOOP(3) { puts("Inside for loop\n"); }
The programmer expects to get the following output from the code:
Inside for loop Inside for loop Inside for loop
But because of the semicolon at the end of the macro definition, the for
loop in the program has a null statement, so the statement "Inside for loop" gets printed just once. Essentially, the semicolon at the end of the macro definition changes the program control flow.
Although this example might not actually be used in code, it shows the effect a semicolon in a macro definition can have.
Compliant Solution
The compliant solution is to write the macro definition without the semicolon at the end, leaving the decision whether or not to have a semicolon up to the person who is using the macro:
#define FOR_LOOP(n) for(i=0; i<(n); i++) int i; FOR_LOOP(3) { puts("Inside for loop\n"); }
Noncompliant Code Example
In this noncompliant code example, the programmer defines a macro that increments the value of the first argument, x
, by 1 and modulates it with the value of the second argument, max
:
#define INCREMOD(x, max) ((x) = ((x) + 1) % (max)); int index = 0; int value; value = INCREMOD(index, 10) + 2; /* ... */
In this case, the programmer intends to increment index
and then use that as a value by adding 2 to it. Unfortunately, the value is equal to the incremented value of index
because of the semicolon present at the end of the macro. The + 2;
is treated as a separate statement by the compiler. The user will not get any compilation errors. If the user has not enabled warnings while compiling, the effect of the semicolon in the macro cannot be detected at an early stage.
Compliant Solution
The compliant solution is to write the macro definition without the semicolon at the end, leaving the decision whether or not to have a semicolon up to the person who is using the macro:
#define INCREMOD(x, max) ((x) = ((x) + 1) % (max))
Compliant Solution
This compliant solution uses an inline function as recommended by PRE00-C. Prefer inline or static functions to function-like macros.
inline int incremod(int *x, int max) {*x = (*x + 1) % max;}
Risk Assessment
Using a semicolon at the end of a macro definition can result in the change of program control flow and thus unintended program behavior.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
PRE11-C | Medium | Probable | Low | P12 | L1 |
Automated Detection
Tool | Version | Checker | Description |
---|---|---|---|
Astrée | 24.04 | macro-final-semicolon | Fully checked |
Axivion Bauhaus Suite | 7.2.0 | CertC-PRE11 | |
CodeSonar | 8.1p0 | LANG.PREPROC.MACROEND | Macro Does Not End With ) or } |
LDRA tool suite | 9.7.1 | 79 S | Enhanced Enforcement |
PC-lint Plus | 1.4 | 823 | Fully supported |
Polyspace Bug Finder | R2024a | CERT C: Rec. PRE11-C | Checks for macros terminated with semicolons (rule fully covered) |
PRQA QA-C | Unable to render {include} The included page could not be found. | 3412 | Partially implemented |
RuleChecker | 24.04 | macro-final-semicolon | Fully checked |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.