28dd489d473ed3e55d765ff7594d43cb5ab1c504 galt Fri Aug 26 17:59:12 2011 -0700 make the range checking for the integer return type be real and prevent overflow diff --git src/lib/options.c src/lib/options.c index c0c41ed..4c55a70 100644 --- src/lib/options.c +++ src/lib/options.c @@ -1,28 +1,29 @@ /* Options.c - stuff to handle command line options. * This is smaller and more flexible than the cgiSpoof * routines we used to use - though cgiSpoof is still the * method of choice for actual CGI routines that want to * be tested from the command line. * * This file is copyright 2002 Jim Kent, but license is hereby * granted for all use - public, private or commercial. */ #include "common.h" #include "hash.h" #include "verbose.h" #include "options.h" +#include <limits.h> static char const rcsid[] = "$Id: options.c,v 1.29 2009/12/02 19:10:38 kent Exp $"; #ifdef MACHTYPE_alpha #define strtoll strtol #endif /* mask for type in optionSpec.flags */ #define OPTION_TYPE_MASK (OPTION_BOOLEAN|OPTION_STRING|OPTION_INT|OPTION_FLOAT|OPTION_LONG_LONG|OPTION_DOUBLE) static struct optionSpec commonOptions[] = { {"verbose", OPTION_INT}, {NULL, 0}, }; @@ -323,39 +324,43 @@ errAbort("ERROR: optionVal cannot be used to get the value of an OPTION_MULTI"); } ret = optGet(name); if (ret == NULL) ret = defaultVal; return ret; } int optionInt(char *name, int defaultVal) /* Return integer value of named option, or default value * if not set. */ { char *s = optGet(name); char *valEnd; -int val; +long lval; if (s == NULL) return defaultVal; if (sameString(s,"on")) return defaultVal; -val = strtol(s, &valEnd, 10); +lval = strtol(s, &valEnd, 10); // use strtol since strtoi does not exist if ((*s == '\0') || (*valEnd != '\0')) errAbort("value of -%s is not a valid integer: \"%s\"", name, s); -return val; +if (lval > INT_MAX) + errAbort("value of -%s is is too large: %ld, integer maximum is %d", name, lval, INT_MAX); +if (lval < INT_MIN) + errAbort("value of -%s is is too small: %ld, integer minimum is %d", name, lval, INT_MIN); +return lval; } long long optionLongLong(char *name, long long defaultVal) /* Return long long value of named option, or default value * if not set. */ { char *s = optGet(name); char *valEnd; long long val; if (s == NULL) return defaultVal; if (sameString(s,"on")) return defaultVal; val = strtoll(s, &valEnd, 10); if ((*s == '\0') || (*valEnd != '\0'))