...
Code Block | ||
---|---|---|
| ||
double currentBalance; // User's cash balance void doDeposit(String userInput){ double val = 0; 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; } |
Exceptions
NUM08-J-EX0: Occasionally, NaN
, infinity
, or -infinity
may be acceptable as expected inputs to a program. In such cases, explicit checks 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.
...
Incorrect or missing validation of floating-point input can result in miscalculations and unexpected results, possibly leading to inconsistent program behavior and denial of service.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
NUM08-J | Low | Probable | Medium | P4 | L3 |
Automated Detection
Automated detection is infeasible in the general case. It could be possible to develop a taint-like analysis that detects many interesting cases.
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Parasoft Jtest |
| CERT.NUM08.FPEXC | Check floating-point inputs for exceptional values |
Related Guidelines
Bibliography
...
...