Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

No cacheing through this lvalue: each operation in the abstract semantics must be performed (that is, no cacheing assumptions may be made, since the location is not guaranteed to contain any previous value). In the absence of this qualifier, the contents of the designated location may be assumed to be unchanged except for possible aliasing.

Non-Compliant Coding Example

If the value of i is cached, the while loop may never terminate, even on the program receiving a SIGINT.

Code Block
bgColor#ffcccc
#include <signal.h> 
 
size_t i; 
 
void handler() { 
  i = 0; 
} 
 
int main(void) { 
  signal(SIGINT, handler); 
  i = 1; 
  while(i) { 
   /* do something */ 
  } 
} 

Compliant Solution

i is accessed for every iteration of the while loop.

Code Block
bgColor#ccccff
#include <signal.h> 
 
volatile size_t i; 
 
void handler() { 
  i = 0; 
} 
 
int main(void) { 
  signal(SIGINT, handler); 
  i = 1; 
  while(i) { 
   /* do something */ 
  } 
}

Risk Assessment

In addition to incorrect optimizations, this can cause race conditions, resulting in inconsistent state.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

DCL34-C

2 (medium)

2 (probable)

3 (low)

P12

L1

References

Wiki Markup
\[[ISO/IEC 9899-1999:TC2|AA. C References#ISO/IEC 9899-1999TC2]\] Section 6.7.3, "Type Qualifiers"
\[[ISO/IEC 03|AA. C References#ISO/IEC 03]\] "Type Qualifiers"
\[[C User's Guide|http://docs.sun.com/source/819-3688/tguide.html#pgfId-997898]\] "Transitioning to ISO C"