At Sat, 27 Jun 2009 18:40:55 +0000, David Holland
<dholland-tech%netbsd.org@localhost> wrote:
Subject: Re: strtonum(3) from OpenBSD?
>
> That's the POINT. Calls that succeed may set errno anyway. You have to
> check that an error occurred before inspecting errno.
Well, with strtol(3) _both_ conditions must hold exactly -- it doesn't
really matter which test is done first! :-)
It should also not matter for strtol(3) which order you check for
overflow, "no digits", and "trailing garbage" conditions either.
> The convention is that no (normal) call *clears errno to 0* on success.
Indeed. Even if you've set errno to zero before making the call there
is no guarantee that it will remain as zero after a call (perhaps not
even if the call is guaranteed to be a direct system call with no other
intervening library calls). Thus with strtol(3) and friends it is
necessary to both set errno to zero (or at least assure it has a value
other than ERANGE :-)) before calling them (assuming you do want to
accept their full range of possible return values) and use the combined
conditions of a min/max return value _and_ errno being set explicitly to
ERANGE to know that there was an overflow.
> A few calls that are specifically required to preserve the prior value
> of errno on success.
Hmmm... I wasn't aware of that. Which?
> Unfortunately, when standards compliance is at issue, sanity has
> little relevance.
:-)
--
Greg A. Woods
Planix, Inc.
<woods%planix.com@localhost> +1 416 218-0099 http://www.planix.com/
Attachment:
pgpZR2RhS7Trv.pgp
Description: PGP signature