Versions Compared

Key

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

Assertions are a valuable diagnostic tool for finding and eliminating software defects that may result in vulnerabilities. (see See guideline MSC11-C. Incorporate diagnostic tests using assertions.) . The runtime assert() macro has some limitations, however, in that it incurs a runtime overhead and, because it calls abort(). Consequently, the runtime assert() macro is only useful for identifying incorrect assumptions and not for runtime error checking. As a result, runtime assertions are generally unsuitable for server programs or embedded systems.

...

According to the C++ 0X draft standard, the constant-expression in a static assert declaration is a constant expression that can be converted to bool at compile time. If the value of the converted expression is true, the declaration has no effect. Otherwise the program is ill-formed, and a diagnostic message (which includes the text of the string-literal) is issued at compile time. For example,

Code Block
/* Passes */
static_assert(
  sizeof(int) <= sizeof(void*), 
  "sizeof(int) <= sizeof(void*)"
); 

/* Fails */
static_assert(
  sizeof(double) <= sizeof(int), 
  "sizeof(double) <= sizeof(int)"
);

...

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

DCL03-C

low

unlikely

high

P1

L3

Automated Detection

Tool

Version

Checker

Description

Section

Compass/ROSE

 

 

Section

could detect violations of this rule merely by looking for calls to assert(), and if it can evaluate the assertion (due to all values being known at compile time), then the code should use static-assert instead.
This assumes ROSE can recognize macro invocation.

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 : DCL03-CPP. Use a static assertion to test the value of a constant expression.

Bibliography

Wiki Markup
\[[Becker 082008|AA. Bibliography#Becker 08]\] 
\[[Eckel 072007|AA. Bibliography#Eckel 07]\]
\[[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\] Section 6.10.1, "Conditional inclusion," and Section 6.10.3.3, "The ## operator," and Section 7.2.1, "Program diagnostics"
\[[Klarer 042004|AA. Bibliography#Klarer 04]\]
\[[Saks 052005|AA. Bibliography#Saks 05]\]
\[[Saks 082008|AA. Bibliography#Saks 08]\]

...