Versions Compared

Key

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

...

Code Block
bgColor#ffcccc

#include <signal.h>&lt;signal.h&gt;

sig_atomic_t i;

void handler(int signum) {
  i = 0;
}

int main(void) {
  i = 1;
  signal(SIGINT, handler);
  while (i) {
   /* do something */
  }
  return 0;
}

...

Code Block
bgColor#ffcccc
#include <signal.h>&lt;signal.h&gt;

sig_atomic_t i;

void handler(int signum) {
  i = 0;
}

int main(void) {
  i = 1;
  signal(SIGINT, handler);
  while (*(volatile sig_atomic_t *)&amp;i) {
   /* do something */
  }
  return 0;
}

...

Code Block
bgColor#ccccff
#include <signal.h>&lt;signal.h&gt;

volatile sig_atomic_t i;

void handler(int signum) {
  i = 0;
}

int main(void) {
  i = 1;
  signal(SIGINT, handler);
  while (i) {
   /* do something */
  }
  return 0;
}

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Other Languages

This rule appears in the C++ Secure Coding Standard as DCL34-CPP. Use volatile for data that cannot be cached.

References

Wiki Markup
\[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.7.3, "&quot;Type qualifiers,"&quot; and Section 7.14, "&quot;Signal handling <signal.h>"&lt;signal.h&gt;&quot;
\[[ISO/IEC 03|AA. C References#ISO/IEC 03]\] Section 6.7.3, "&quot;Type qualifiers"&quot;
\[[Sun 05|AA. C References#Sun 05]\] [Chapter 6, "&quot;Transitioning to ISO C"&quot;|http://docs.sun.com/source/819-3688/tguide.html#pgfId-997898]

...

DCL33-C. Ensure that restrict-qualified source and destination pointers in function arguments do not reference overlapping objects      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;02. Declarations and Initialization (DCL)       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DCL35-C. Do not invoke a function using a type that does not match the function definition