Subject: Re: tales of woe from the recent tools/compat_defs.h pullups on netbsd-1-6
To: NetBSD Toolchain Technical Discussion List <tech-toolchain@NetBSD.org>
From: Greg A. Woods <woods@weird.com>
List: tech-toolchain
Date: 06/23/2004 03:02:44
[ On Tuesday, June 22, 2004 at 09:57:41 (+0200), Alan Barrett wrote: ]
> Subject: Re: tales of woe from the recent tools/compat_defs.h pullups on netbsd-1-6
>
> strtoul() and friends are documented to set errno = ERANGE on overflow.
> I had a quick look at the implementation, and don't see any obvious
> bugs.  So you should be able to do something like this:
> 
> 	errno = 0;
> 	result = strtoul(...);
> 	if (result == ULONG_MAX && errno == ERANGE) {
> 	   ...
> 	}

Hmmm....  I suppose so.  I'm just _really_ leary about setting errno
before making a library call and then expecting it to only be changed
iff there's an error, even I use a "special" result in that way as a
hint to check errno only when it might be necessary to do so.

(and thats without even considering the implications of errno in a
threaded environment!  ;-)

With the strtod() family of APIs I generally prefer to only ever use the
state of endptr for error detection.  If it doesn't point to the NUL
byte at the end of my intput string then I know something went wrong.  I
suppose I could use the hint that if it points to a digit then the
conversion probably overflowed, but I'm not sure I trust that as much as
I trust a complete successful conversion that returns a value which
would overflow if it were cast to the final size of integer.


-- 
						Greg A. Woods

+1 416 218-0098                  VE3TCP            RoboHack <woods@robohack.ca>
Planix, Inc. <woods@planix.com>          Secrets of the Weird <woods@weird.com>