An object that has volatile-qualified type may be modified in ways unknown to the implementation or have other unknown side effects. It is possible to reference a volatile object by using a non-volatile value, but the resulting behavior is undefined. According to ISO/IEC 9899:1999, Section 6.7.3 "Type Qualifiers" (see also undefined behavior 62 of Appendix J):
If an attempt is made to refer to an object defined with a volatile-qualified type through use of an lvalue with non-volatile-qualified type, the behavior is undefined.
(See also undefined behavior 62 of Appendix J.)
Noncompliant Code Example
...
This example compiles without warning on Microsoft Visual C++ .NET (2003) and on MS Visual Studio 2005.
This example does not compile on MS Visual Studio 2008. The error message is
...
In this compliant solution, ip
is declared as 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) { /* ... */ } |
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
EXP32-C | low | likely | medium | P6 | L2 |
Automated Detection
Tool |
---|
...
Version | Checker | Description | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
|
| ||||||||||
|
...
|
|
|
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Other Languages
Related Guidelines
This rule appears in the C++ Secure Coding Standard as : EXP32-CPP. Do not access a volatile object through a non-volatile reference.
Bibliography
Wiki Markup |
---|
\[[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\] Section 6.7.3, "Type qualifiers," and Section 6.5.16.1, "Simple assignment" \[[ISO/IEC PDTR 24772|AA. Bibliography#ISO/IEC PDTR 24772]\] "HFC Pointer casting and pointer type changes" and "IHN Type system" \[[MISRA 042004|AA. Bibliography#MISRA 04]\] Rule 11.5 |
...