Versions Compared

Key

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

Floating-point numbers can take on two classes of exceptional values; infinity and NaN (not-a-number). These values are returned as the result of exceptional or otherwise unresolvable floating point operations. (See also : guideline FLP32-C. Prevent or detect domain and range errors in mEath math functions.) . Additionally, they can be directly input by a user by scanf or similar functions. Failure to detect and handle such values can result in undefined behavior.

NaN values are particularly problematic, as the expression NaN==NaN (for every possible value of NaN) returns false. Any comparisons made with NaN as one of the arguments returns false, and all arithmetic functions on NaNs simply propagate them through the code. Hence, a NaN entered in one location in the code and not properly handled could potentially cause problems in other, more distant sections.

...

The following noncompliant code accepts user data without first validating it.

Code Block
bgColor#FFCCCC

float currentBalance; /* User's cash balance */
void doDeposit() {
  float val;

  scanf("%f", &val);

  if(val >= MAX_VALUE - currentBalance) {
    /* Handle range error */
  }

  currentBalance += val;
}

...

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

Other Languages

Related Guidelines

This rule appears in the Java Secure Coding Standard as FLP04: FLP06-J. Check floating point inputs for exceptional values This rule appears in the

C++ Secure Coding Standard as : FLP04-CPP. Check floating point inputs for exceptional values.

Bibliography

Wiki Markup
\[[IEEE 754|AA. Bibliography#IEEE 754 2006]\]
\[[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\]
\[[IEEE 1003.1, 2004|AA. Bibliography#IEEE 1003]\]

...