Versions Compared

Key

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

...

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

Code Block
bgColor#FFCCCC

double currentBalance; /* User's cash balance */

void doDeposit(){
  double val;

  scanf("%g", &val);

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

  currentBalance+=val;
}

This can be a problem if an invalid value is entered for val and subsequently used for calculations or as control values. The user could, for example, input the strings "INF", "INFINITY", or "NAN" (case insensitive) on the command line, which would be parsed by scanf into the floating-point representations of infinity and NaN. All subsequent calculations using these values would be invalid, possibly crashing the program and enabling a DOS attack.

...

Code Block
bgColor#ccccff
double currentBalance; /* User's cash balance */

void doDeposit(){
  double val;

  scanf("%g", &val);

  int k=isinf(x);

  if (k==1){

    /* handle infinity error */

  }

  if (k==-1){

    /* handle negative infinity error */

  }

  if(isnan(val)) /* test NaN */

  {

    /* handle NaN error */

  }

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

  currentBalance+=val;
}

Exceptions

...