...
The following definition for static_assert()
from recommendation DCL03-C. Use a static assertion to test the value of a constant expression uses the JOIN()
macro to concatenate the token assertion_failed_at_line_
with the value of __LINE__
.
Code Block |
---|
#define static_assert(e) \
typedef char JOIN(assertion_failed_at_line_, __LINE__) \
[(e) ? 1 : -1]
|
__LINE__
is a predefined macro names which expands to an integer constant representing the presumed line number of the current source line within the current source file [ISO/IEC 9899:19992011].
If the intention is to expand the __LINE__
macro, which is likely the case here, the following definition for JOIN()
is noncompliant:
Code Block | ||||
---|---|---|---|---|
| ||||
#define JOIN(x, y) x ## y
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
#define JOIN(x, y) JOIN_AGAIN(x, y)
#define JOIN_AGAIN(x, y) x ## y
|
...
Note also that macro parameters cannot be individually parenthesized when concatenating tokens using the ##
operator, converting macro parameters to strings using the #
operator, or concatenating adjacent string literals. This is an exception, PRE01-EX2, to recommendation PRE01-C. Use parentheses within macros around parameter names.
...
Code Block | ||||
---|---|---|---|---|
| ||||
#define str(s) #s
#define foo 4
str(foo)
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
#define xstr(s) str(s)
#define str(s) #s
#define foo 4
|
...
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
PRE05-C | low | unlikely | medium | P2 | L3 |
Automated Detection
Tool | Version | Checker | Description | section|||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
| 125 SS | Section | Fully Implementedimplemented |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...
CERT C++ Secure Coding Standard: PRE05-CPP. Understand macro replacement when concatenating tokens or performing stringification
ISO/IEC 9899:19992011 Section 6.10.3, "Macro replacement," Section 6.10.3.32, "The ##
#
operator," Section 6.10.3.23, "The #
##
operator," Section 6.10.3.4, "Rescanning and further replacement," and Section 6.10.8, "Predefined macro names"
...
[FSF 2005] Section 3.4, "Stringification" and Section 3.5, "Concatenation"
[Saks 2008]
...