...
The following noncompliant code accepts user data without first validating it.
Code Block | ||||
---|---|---|---|---|
| ||||
float currentBalance; /* User's cash balance */ void doDeposit() { float val; scanf("%f", &val); if(val >= MAX_VALUE - currentBalance) { /* Handle range error */ } currentBalance += val; } |
...
The following code first validates the input float before using it. The value is tested to ensure that it is neither an infinity nor a NaN.
Code Block | ||||
---|---|---|---|---|
| ||||
float currentBalance; /* User's cash balance */ void doDeposit() { float val; scanf("%f", &val); if (isinf(val)) { /* handle infinity error */ } if (isnan(val)) { /* handle NaN error */ } if (val >= MAX_VALUE - currentBalance) { /*Handle range error*/ } currentBalance += val; } |
...