tech-userlevel archive

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

Re: strtonum(3) from OpenBSD?

>> [...], but it does matter which test is done first if errno might
>> contain a trap representation.
> I meant in the strict ordering of terms in the test expression(s).

>       if (errno == ERANGE && rval == LONG_MAX)

>       if (rval == LONG_MAX && errno == ERANGE)

These are not equivalent if errno might contain a trap representation
for non-LONG_MAX returns.  (Because of strtol()'s special case for
errno, this can happen only if it was that way on entry.)

>> [...] if strtol were allowed to leave trash in errno, [...]
> Hmmm... yes, but _only_ if that "trash" value were ERANGE (or the
> undocumented EINVAL return).  Trash has a nasty habit of being what you least want it to
at the most inconvenient times.

> If I understand you correctly, you are suggesting that ERANGE may be
> inadvertently set by some arithmetic operation during the conversion
> of a string that represents _exactly_ LONG_MAX.

Not necessarily _arithmetic_ operation.  There is no guarantee that
strtol() and friends are leaf routines, and, indeed, it's not
implausible that they may do something (like locale setup) which may
leave relatively unpredictable trash in errno.

But, since there is no other way to disambiguate LONG_MAX, strtol()'s
interface contract includes special mention of errno - namely, that a
successful conversion leaves errno set to whatever it was on entry.

/~\ The ASCII                             Mouse
\ / Ribbon Campaign
 X  Against HTML      
/ \ Email!           7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B

Home | Main Index | Thread Index | Old Index