...
In this example, a volatile object is accessed through a non-volatile-qualified reference, resulting in undefined behavior.:
Code Block | ||||
---|---|---|---|---|
| ||||
static volatile int **ipp; static int *ip; static volatile int i = 0; printf("i = %d.\n", i); ipp = &ip; /* produces warnings in modern compilers */ ipp = (int**) &ip; /* constraint violation, also produces warnings */ *ipp = &i; /* valid */ if (*ip != 0) { /* valid */ /* ... */ } |
...
In this compliant solution, ip
is declared volatile.:
Code Block | ||||
---|---|---|---|---|
| ||||
static volatile int **ipp; static volatile int *ip; static volatile int i = 0; printf("i = %d.\n", i); ipp = &ip; *ipp = &i; if (*ip != 0) { /* ... */ } |
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Compass/ROSE |
|
|
| ||||||
GCC |
|
| Can detect violations of this rule when the | ||||||
| 344 S | Fully implemented. | |||||||
PRQA QA-C |
| 0312 | Fully implemented. |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...
CERT C++ Secure Coding Standard | EXP32-CPP. Do not access a volatile object through a non-volatile reference |
ISO/IEC TR 24772:2013 | Pointer Casting and Pointer Type Changes [HFC] Type System [IHN] |
MISRA - C:2012 | Rule 11.58 (required) |
Bibliography
[ISO/IEC 9899:2011] | Section 6.7.3, "Type Qualifiers" |
...