tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: strtonum(3) from OpenBSD?

On Sat, Jun 27, 2009 at 10:26:49PM -0400, Greg A. Woods wrote:
 > > 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!  :-)

In all cases, not just strtol, both conditions must hold, but it does
matter which test is done first if errno might contain a trap

The problem is that if strtol were allowed to leave trash in errno,
and the number you were converting was exactly LONG_MAX, you might
incorrectly conclude that an overflow occurred.

However, as has been noted elsewhere by now, it's not allowed to leave
trash, which in turn means that there's no particular reason to test
for LONG_MAX or LONG_MIN; provided you clear errno beforehand it's
sufficient to test errno afterwards.

(Unless you want to distinguish a "positive value too large" overflow
from "negative value too large" overflow, but I don't think that's the
common case.)

 > > A few calls that are specifically required to preserve the prior value
 > > of errno on success.

[s/that// I guess]

 > Hmmm...  I wasn't aware of that.  Which?

strtol, for one :-)

David A. Holland

Home | Main Index | Thread Index | Old Index