You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Jim Jagielski <ji...@jaguNET.com> on 2002/06/19 16:08:59 UTC

Simple test for strtol

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