misc/69788: The processing result of strtoul() cannot judge whether it is an error.

SAKUMA takayuki takayuki at finet.fujitsu.com
Fri Jul 30 00:31:34 PDT 2004


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

From: SAKUMA takayuki <takayuki at finet.fujitsu.com>
To: sos22 at cantab.net
Cc: freebsd-gnats-submit at FreeBSD.org, sos22 at srcf.ucam.org
Subject: Re: misc/69788: The processing result of strtoul() cannot judge
 whether it is an error.
Date: Fri, 30 Jul 2004 16:24:42 +0900 (JST)

 > In my tests, strtoul correctly leaves errno at 0:
 > 	errno=0;
 > 	ul = strtoul("4294967295", &p, 10);
 > 	o_errno = errno;	
 > 	printf("ul=%lu,*p=%x,errno=%d(%s)\n", ul, *p, o_errno,strerror(errno));
 >  
 > gives the result ``ul=4294967295,*p=0,errno=0(Unknown error: 0)'', as
 > expected.  The problem with the original test case is that strerror()
 > is setting errno to EINVAL (because 0 isn't a valid error number)
 > before errno is pushed on the stack for printf (the order in which
 > function arguments are evaluated is undefined in C).
 
 ul = strtoul("4294967296", &p, 10);
 ul = strtoul("4294967295", &p, 10);
 
 2nd strtoul() will success, but errno == ERANGE.
 Is it necessary to set errno as 0 before calling 2nd strtoul() ?
 
 ------
 takayuki at finet.fujitsu.com


More information about the freebsd-bugs mailing list