...
In this example, a volatile object is accessed through a non-volatile-qualified reference resulting in undefined behavior.
Code Block |
---|
int main(void) { static volatile int **ipp; static int *ip; static volatile int i = 0;; printf("i = %d.\n", i); ipp = &ip; //* constraint violation */ *ipp = &i; //* valid */ if (*ip != 0) { //* valid // i had been changed */ ... } } |
The first assignment ipp = &ip
is unsafe because it would allow the following valid code to reference the value of the volatile object i
through a the non-volatile qualified reference ip
. In this example, the compiler may optimize out the entire if block because there it is not possible that i == 0
if i
is not volatile.
Implementation Specific Details
...
In this compliant solution the int * , ip
is declared as volatile.
Code Block |
---|
int main(void) { static volatile int **ipp; static volatile int *ip; static volatile int i = 0;; printf("i = %d.\n", i); ipp = &ip; // constraint violation *ipp = &i; // valid if (*ip != 0) { // valid /* i has changed */... } |
Priority:
...
P6 Level:
...
L2
Accessing a volatile object through a non-volatile reference results in undefined behavior.
Component | Value |
---|---|
Severity | 1 (low) |
Likelihood | 1 3 (unlikely) |
Remediation cost | 2 (medium) |
...