httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jim Jagielski <...@jaguNET.com>
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
    ULONG_MAX is > LONG_MAX
*/
main() {
  char c[256];
  long l;
  printf("Overflow test:\n");
  snprintf(c,255,"%uld",ULONG_MAX);
  errno = 0;
  l = strtol(c,NULL,10);
  if (errno)
    printf(" GOOD! Captured the overflow\n");
  else
    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");
  snprintf(c,255,"%uld",LONG_MAX);
  errno = 0;
  l = strtol(c,NULL,10);
  if (errno)
    printf(" BAD! Bogus overflow detected\n");
  else
    printf(" GOOD! Bogus overflow not detected\n");
  printf(" Result is %ld (%lx)\n", l, l);
}
-- 
===========================================================================
   Jim Jagielski   [|]   jim@jaguNET.com   [|]   http://www.jaguNET.com/
      "A society that will trade a little liberty for a little order
             will lose both and deserve neither" - T.Jefferson

Mime
View raw message