diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2007-12-09 17:30:47 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2007-12-09 17:30:47 +0000 |
commit | d97e2f3a64b09a748bb0984fba33f3595298853a (patch) | |
tree | d5f388bde91bdc4ecad19f87c757c2790801ce69 /lib/util/nstring.c | |
parent | b4799443c85bee0e0afc1fa23e034d2253a07635 (diff) | |
download | netpbm-mirror-d97e2f3a64b09a748bb0984fba33f3595298853a.tar.gz netpbm-mirror-d97e2f3a64b09a748bb0984fba33f3595298853a.tar.xz netpbm-mirror-d97e2f3a64b09a748bb0984fba33f3595298853a.zip |
fix overflow calculation
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@479 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/util/nstring.c')
-rw-r--r-- | lib/util/nstring.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/util/nstring.c b/lib/util/nstring.c index feb8215e..3e2da669 100644 --- a/lib/util/nstring.c +++ b/lib/util/nstring.c @@ -915,3 +915,40 @@ strishex(const char * const subject) { return retval; } + + + +void +interpret_uint(const char * const string, + unsigned int * const valueP, + const char ** const errorP) { + + if (string[0] == '\0') + asprintfN(errorP, "Null string."); + else { + /* strtoul() does a bizarre thing where if the number is out + of range, it returns a clamped value but tells you about it + by setting errno = ERANGE. If it is not out of range, + strtoul() leaves errno alone. + */ + char * tail; + unsigned long ulongValue; + + errno = 0; // So we can tell if strtoul() overflowed + + ulongValue = strtoul(string, &tail, 10); + + if (tail[0] != '\0') + asprintfN(errorP, "Non-digit stuff in string: %s", tail); + else if (errno == ERANGE) + asprintfN(errorP, "Number too large"); + else if (ulongValue > UINT_MAX) + asprintfN(errorP, "Number too large"); + else { + *valueP = ulongValue; + *errorP = NULL; + } + } +} + + |