Floating-point numbers can take on two exceptional values, infinity
and NaN
(not-a-number). These values are produced as a result of exceptional or, otherwise, unresolvable floating point operations. Additionally, they may can be obtained directly from user input through methods like Double.valueOf(String s)
. Failure to detect and handle such values can result in inconsistent behavior.
NaN
values are particularly problematic because they are unordered; that is, the expression NaN == NaN
always returns false
. (see See guideline FLP05-J. Do not attempt comparisons with NaN.) In general, any comparisons with NaN
return false
, and all arithmetic functions with one or more NaN
inputs produce NaN
as their output. Consequently, a single occurrence of a NaN
value can cause regressions within other code segments. This correct — and arguably desirable — behavior correctâ”and arguably desirableâ”behavior can cause unexpected results.
The method Double.valueOf(String s)
can return NaN
or an infinite double
, as specified by its contract. Programs should install checks to ensure that all floating point inputs (especially those obtained from the user) are free of unexpected exceptional values. The methods Double.isNaN(double d)
and Double.isInfinite(double d)
can be used for this purpose.
...
This compliant solution validates the floating point input before using it. The value is tested to ensure that it is neither infinity
, negative infinity
, nor NaN
.
Code Block | ||
---|---|---|
| ||
double currentBalance; // User's cash balance void doDeposit(String s){ double val; try { val = Double.valueOf(userInput); } catch(NumberFormatException e) { // Handle input format error } if (Double.isInfinite(val)){ // Handle infinity error } if (Double.isNaN(val)) { // Handle NaN error } if (val >= Double.MAX_VALUE - currentBalance) { // Handle range error } currentBalance += val; } |
...
FLP06-EX1: Occasionally, NaN
or infinity may be acceptable as expected inputs to a program. In such cases, explicit checks may might not be necessary. However, such programs must be prepared to handle these exceptional values gracefully , and should prevent propagation of the exceptional values to other code that fails to handle exceptional values. The choice to permit input of exceptional values during ordinary operation should be explicitly documented.
...
Automated detection is not feasible in the general case. It may could be possible to develop a taint-like analysis that detects many interesting cases.
...
Other Languages
Related Guidelines
This guideline appears in the C Secure Coding Standard as : FLP04-C. Check floating point inputs for exceptional valuesThis
guideline appears in the C++ Secure Coding Standard as : FLP04-CPP. Check floating point inputs for exceptional values.
Bibliography
Wiki Markup |
---|
\[[IEEE 754|https://www.securecoding.cert.org/confluence/display/seccode/AA.+C+References#AA.CReferences-IEEE7542006|IEEE 754]\] \[[IEEE 1003.1, 2004|https://www.securecoding.cert.org/confluence/display/seccode/AA.+C+References#AA.CReferences-IEEE1003|IEEE 1003.1, 2004]\] |
...