misc/135932: 'strtol' doesn't reset errno to 0 when converting MAX_INT=2147483647

Bruce Evans brde at optusnet.com.au
Tue Jun 23 03:20:05 UTC 2009


The following reply was made to PR misc/135932; it has been noted by GNATS.

From: Bruce Evans <brde at optusnet.com.au>
To: Yuri <yuri at tsoft.com>
Cc: freebsd-gnats-submit at FreeBSD.org, freebsd-bugs at FreeBSD.org
Subject: Re: misc/135932: 'strtol' doesn't reset errno to 0 when converting
 MAX_INT=2147483647
Date: Tue, 23 Jun 2009 12:02:12 +1000 (EST)

 On Mon, 22 Jun 2009, Yuri wrote:
 
 >> Description:
 > When strtol is supplied string 2147483647 it's impossible to distinguish between overflow and non-overflow situation since return value is the same one that flags overflow (MAX_INT) and strtol doesn't clear errno in this case.
 
 No, this is easy to distinguish: set errno to 0 (or just to some value
 different from ERANGE) before calling strtol(), and check errno after
 calling strtol().  The check can be omitted unless strtol() actually
 returns INT_MAX.  The setting before the call can only be omitted it
 this error can't happen or if the caller doesn't check for it.  Most
 uses of the strtol() family get this wrong by not even checking :-(.
 
 > strtol should set errno=0 in this case to avoid ambiguity.
 
 No, strtol() must not set errno to 0.  The C standard doesn't permit any
 library function to set errno to 0.
 
 Bruce


More information about the freebsd-bugs mailing list