Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Fixed strtol() error handling (the code used to treat a successful return of LONG_MIN or LONG_MAX as an error)

...

Code Block
bgColor#ccccff
long sl;
int si;
char *end_ptr;

if (argc > 1) {
  errno = 0;

  sl = strtol(argv[1], &end_ptr, 10);

  if ((sl == LONG_MIN)
   || (sl == LONG_MAX) 
   || (end_ptr == argv[1]))
  {
    if (&& errno != 0)
  {
      perror("strtol error");
  }
  }
else if (end_ptr  else== argv[1]) {
      if (puts("error encountered during conversion") == EOF) {
        /* Handle Error */
      }
    }
  }
  else if (sl > INT_MAX) {
    printf("%ld too large!\n", sl);
  }
  else if (sl < INT_MIN) {
    printf("%ld too small!\n", sl);
  }
  else if ('\0' != *end_ptr) {
    if (puts("extra characters on input line\n") == EOF) {
      /* Handle Error */
    }
  }
  else {
    si = (int)sl;
  }
}

...