...
In this noncompliant code example, the expression a++
is not evaluated, and the side effects in the expression are not executed.:
Code Block | ||||
---|---|---|---|---|
| ||||
int a = 14; int b = sizeof(a++); |
...
In this compliant solution, the variable a
is incremented.:
Code Block | ||||
---|---|---|---|---|
| ||||
int a = 14; int b = sizeof(a); a++; |
...
This compliant solution avoids changing the value of the variable n
used in the sizeof
expression and instead increments it safely outside of it.:
Code Block | ||||
---|---|---|---|---|
| ||||
void f(size_t n) { size_t a = sizeof(int [n + 1]); ++n; size_t b = sizeof(int [n % 1 + 1]); ++n; /* ... */ } |
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Compass/ROSE |
|
|
| ||||||
| unexfct | Fully implemented. | |||||||
| 54 S | Fully implemented | |||||||
PRQA QA-C |
| 3307 | Fully implemented |
...
CERT C++ Secure Coding Standard | EXP06-CPP. Operands to the sizeof operator should not contain side effects |
...