Macro terminated with a semicolon
Description
This defect occurs when a macro that is invoked at least once has a definition ending with a semicolon.
Risk
If a macro definition ends with a semicolon, the macro expansion can lead to unintended program logic in certain contexts, such as within an expression.
For instance, consider the macro:
#define INC_BY_ONE(x) ++x;
res = INC_BY_ONE(x)%2;
res = ++x; %2;
x+1
is
assigned to res
, which is probably unintended. The leftover standalone
statement %2;
is valid C code and can only be detected by enabling strict
compiler warnings.Fix
Do not end macro definitions with a semicolon. Leave it up to users of the macro to add a semicolon after the macro when needed.
Alternatively, use inline functions in preference to function-like macros that involve statements ending with semicolon.
Examples
Result Information
Group: Good practice |
Language: C | C++ |
Default: Off |
Command-Line Syntax:
SEMICOLON_TERMINATED_MACRO |
Impact: Low |
Version History
Introduced in R2020a
See Also
Find defects
(-checkers)
| Incorrectly indented
statement
| Semicolon on same line as if, for or
while statement
| Macro with multiple
statements
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)