httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jim Jagielski <>
Subject Simple test for strtol
Date Wed, 19 Jun 2002 14:08:59 GMT
Here is a simple test that checks to see if strtol correctly handles an
overflow condition. Doesn't check boundary conditions per se (-1/0/+1
LONG_MAX, eg) but is adequate for a configure test, I think. (Well,
not as is, since it very verbose, but logic-wise).

#include <stdio.h>
#include <limits.h>
#include <errno.h>
/* Assumptions:
    ULONG_MAX, in decimal, has <255 digits
    LONG_MAX, in decimal, has <255 digits
main() {
  char c[256];
  long l;
  printf("Overflow test:\n");
  errno = 0;
  l = strtol(c,NULL,10);
  if (errno)
    printf(" GOOD! Captured the overflow\n");
    printf(" BAD! Failed to capture the overflow\n");
  if (l == LONG_MAX)
    printf(" Result set to LONG_MAX\n");
  printf(" Result is %ld (%lx)\n", l, l);
  printf("Bogus Overflow test:\n");
  errno = 0;
  l = strtol(c,NULL,10);
  if (errno)
    printf(" BAD! Bogus overflow detected\n");
    printf(" GOOD! Bogus overflow not detected\n");
  printf(" Result is %ld (%lx)\n", l, l);
   Jim Jagielski   [|]   [|]
      "A society that will trade a little liberty for a little order
             will lose both and deserve neither" - T.Jefferson

View raw message