...
Note the comment warning programmers that the macro is unsafe. The macro can also be renamed ABS_UNSAFE() to make it clear that the macro is unsafe. This compliant solution, like all the compliant solutions for this rule, has undefined behavior (see undefined behavior 36) if the argument to ABS() is equal to the minimum (most negative) value for the signed integer type. (See INT32-C. Ensure that operations on signed integers do not result in overflow for more information.)
...
This compliant solution follows the guidance of PRE00-C. Prefer inline or static functions to function-like macros by defining an inline function iabs() to replace the ABS() macro. Unlike the ABS() macro, which operates on operands of any type, the iabs() function will truncate arguments of types wider than int whose value is not in range of the latter type.
...
According to the C Standard, 6.5.12.1, paragraph 3 [ISO/IEC 9899:20112024]:
The controlling expression of a generic selection is not evaluated. If a generic selection has a generic association with a type name that is compatible with the type of the controlling expression, then the result expression of the generic selection is the expression in that generic association. Otherwise, the result expression of the generic selection is the expression in the
defaultgeneric association. None of the expressions from any other generic association of the generic selection is evaluated.
Because the expression is not evaluated as part of the generic selection, the use of a macro in this solution is guaranteed to evaluate the macro parameter v only once.
...
Invoking an unsafe macro with an argument that has side effects may cause those side effects to occur more than once. This practice can lead to unexpected program behavior.
Rule | Severity | Likelihood |
|---|
Detectable | Repairable | Priority | Level |
|---|---|---|---|
PRE31-C | Low | Unlikely |
No | Yes |
P2 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||||
|---|---|---|---|---|---|---|---|---|---|
| Astrée |
| expanded-side-effect-multiplied | Partially checked | ||||||
| Axivion Bauhaus Suite |
| CertC-PRE31 | Fully implemented | ||||||
| CodeSonar |
| LANG.PREPROC.FUNCMACRO | Function-Like Macro | ||||||
| ASSERT_SIDE_EFFECTS | Partially implemented Can detect the specific instance where assertion contains an operation/function call that may have a side effect | |||||||
| Cppcheck Premium |
| premium-cert-pre31-c | |||||||
| ECLAIR |
| CC2.EXP31 CC2.PRE31 | Fully implemented | ||||||
| Helix QAC |
| C3462, C3463, C3464,C3465,C3466,C3467 C++3225, C++3226, C++3227, C++3228, C++3229 | Fully implemented | ||||||
| Klocwork |
| PORTING.VAR.EFFECTS | Fully implemented | ||||||
| LDRA tool suite |
| 9 S, 562 S, 572 S, 35 D, 1 Q | Fully implemented | ||||||
| Parasoft C/C++test |
| CERT_C-PRE31-b | Assertions should not contain assignments, increment, or decrement operators | |||||||
| PC-lint Plus |
| 666, 2666 | Fully supported | ||||||
| Polyspace Bug Finder |
| CERT C: Rule PRE31-C | Checks for side effect in arguments to unsafe macro (rule partially covered) | ||||||
| RuleChecker |
| expanded-side-effect-multiplied | Partially checked | ||||||
| Security Reviewer - Static Reviewer |
| RTOS_28 | Fully implemented |
3454, 3455, 3456
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
Key here (explains table format and definitions)
Taxonomy | Taxonomy item | Relationship |
|---|---|---|
| CERT C | PRE00-C. Prefer inline or static functions to function-like macros | Prior to 2018-01-12: CERT: Unspecified Relationship |
| CERT C | PRE12-C. Do not define unsafe macros | Prior to 2018-01-12: CERT: Unspecified Relationship |
| CERT C | MSC14-C. Do not introduce unnecessary platform dependencies | Prior to 2018-01-12: CERT: Unspecified Relationship |
| CERT C | DCL37-C. Do not declare or define a reserved identifier | Prior to 2018-01-12: CERT: Unspecified Relationship |
| CERT C | PRE31-CPP. Avoid side-effects in arguments to unsafe macros | Prior to 2018-01-12: CERT: Unspecified Relationship |
| CERT Oracle Secure Coding Standard for Java | EXP06-J. Expressions used in assertions must not produce side effects | Prior to 2018-01-12: CERT: Unspecified Relationship |
| ISO/IEC TR 24772:2013 | Pre-processor Directives [NMP] | Prior to 2018-01-12: CERT: Unspecified Relationship |
| MISRA C:2012 | Rule 20.5 (advisory) | Prior to 2018-01-12: CERT: Unspecified Relationship |
Key here (explains table format and definitions)
Taxonomy | Taxonomy item | Relationship |
|---|
Bibliography
| [Dewhurst 2002] | Gotcha #28, "Side Effects in Assertions" |
| [ISO/IEC 9899: |
| 2024] | Subclause 6.5. |
| 2.1, "Generic Selection" | |
| [Plum 1985] | Rule 1-11 |
...
...