From nobody@FreeBSD.org  Fri Jul 30 06:14:54 2004
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id B51FB16A4CE
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 30 Jul 2004 06:14:54 +0000 (GMT)
Received: from www.freebsd.org (www.freebsd.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id AA0A743D49
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 30 Jul 2004 06:14:54 +0000 (GMT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.12.11/8.12.11) with ESMTP id i6U6E2sc087851
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 30 Jul 2004 06:14:02 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.12.11/8.12.11/Submit) id i6U6E2Ps087850;
	Fri, 30 Jul 2004 06:14:02 GMT
	(envelope-from nobody)
Message-Id: <200407300614.i6U6E2Ps087850@www.freebsd.org>
Date: Fri, 30 Jul 2004 06:14:02 GMT
From: Takayuki Sakuma <takayuki@finet.fujitsu.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: The processing result of strtoul() cannot judge whether it is an error.
X-Send-Pr-Version: www-2.3

>Number:         69788
>Category:       misc
>Synopsis:       The processing result of strtoul() cannot judge whether it is an error.
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Jul 30 06:20:12 GMT 2004
>Closed-Date:    Fri Jul 30 10:12:29 GMT 2004
>Last-Modified:  Fri Jul 30 10:12:29 GMT 2004
>Originator:     Takayuki Sakuma
>Release:        4.10-RELEASE
>Organization:
>Environment:
FreeBSD moon.finet.fujitsu.co.jp 4.10-RELEASE FreeBSD 4.10-RELEASE #0: Tue Jul  6 12:59:40 JST 2004     root@moon.finet.fujitsu.co.jp:/usr/src/sys/compile/withDVD  i386
>Description:
The processing result of strtoul() cannot judge whether it is an error when errno has already set to ERANGE before calling this function.
>How-To-Repeat:
void
main(void)
{
        unsigned long ul;
        char *p;

        ul = strtoul("4294967296", &p, 10);
        printf("ul=%lu,*p=%x,errno=%d(%s)\n", ul, *p, errno, strerror(errno));

        ul = strtoul("4294967295", &p, 10);
        printf("ul=%lu,*p=%x,errno=%d(%s)\n", ul, *p, errno, strerror(errno));

        errno=0;
        ul = strtoul("4294967295", &p, 10);
        printf("ul=%lu,*p=%x,errno=%d(%s)\n", ul, *p, errno, strerror(errno));
}

result:
ul=4294967295,*p=0,errno=34(Result too large)
ul=4294967295,*p=0,errno=34(Result too large)
ul=4294967295,*p=0,errno=22(Unknown error: 0)

>Fix:

>Release-Note:
>Audit-Trail:

From: Steven Smith <sos22@cantab.net>
To: Takayuki Sakuma <takayuki@finet.fujitsu.com>
Cc: freebsd-gnats-submit@FreeBSD.org, sos22@srcf.ucam.org
Subject: Re: misc/69788: The processing result of strtoul() cannot judge whether it is an error.
Date: Fri, 30 Jul 2004 08:12:42 +0100

 --liOOAslEiF7prFVr
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 Content-Transfer-Encoding: quoted-printable
 
 >         errno=3D0;
 >         ul =3D strtoul("4294967295", &p, 10);
 >         printf("ul=3D%lu,*p=3D%x,errno=3D%d(%s)\n", ul, *p, errno, strerr=
 or(errno));
 >=20
 > result:
 > ul=3D4294967295,*p=3D0,errno=3D22(Unknown error: 0)
 
 In my tests, strtoul correctly leaves errno at 0:
 	errno=3D0;
 	ul =3D strtoul("4294967295", &p, 10);
 	o_errno =3D errno;=09
 	printf("ul=3D%lu,*p=3D%x,errno=3D%d(%s)\n", ul, *p, o_errno,strerror(errno=
 ));
 =20
 gives the result ``ul=3D4294967295,*p=3D0,errno=3D0(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).
 
 Steven Smith,
 sos22@cantab.net.
 --=20
 One day, I'm going to get an Alice-bot to answer all my email for me,
 and see how long it takes people to notice.
 
 --liOOAslEiF7prFVr
 Content-Type: application/pgp-signature
 Content-Disposition: inline
 
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.2.4 (FreeBSD)
 
 iD8DBQFBCfTqO4S8/gLNrjcRAjpOAJ9NjV/uXuyDyTVjZgSo3vO3NubF2gCfVHo6
 wmgzDCyCrVNPkA9VvQTbwi8=
 =etGX
 -----END PGP SIGNATURE-----
 
 --liOOAslEiF7prFVr--

From: SAKUMA takayuki <takayuki@finet.fujitsu.com>
To: sos22@cantab.net
Cc: freebsd-gnats-submit@FreeBSD.org, sos22@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@finet.fujitsu.com

From: Steven Smith <sos22@cantab.net>
To: SAKUMA takayuki <takayuki@finet.fujitsu.com>
Cc: freebsd-gnats-submit@FreeBSD.org, sos22@srcf.ucam.org
Subject: Re: misc/69788: The processing result of strtoul() cannot judge whether it is an error.
Date: Fri, 30 Jul 2004 10:17:58 +0100

 --9jxsPFA5p3P2qPhR
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 Content-Transfer-Encoding: quoted-printable
 
 > ul =3D strtoul("4294967296", &p, 10);
 > ul =3D strtoul("4294967295", &p, 10);
 >=20
 > 2nd strtoul() will success, but errno =3D=3D ERANGE.
 > Is it necessary to set errno as 0 before calling 2nd strtoul() ?
 Yes: a succesful library call leaves errno unchanged, rather than
 setting it to 0 (usually).
 
 Steven Smith,
 sos22@cantab.net.
 --=20
 One day, I'm going to get an Alice-bot to answer all my email for me,
 and see how long it takes people to notice.
 
 --9jxsPFA5p3P2qPhR
 Content-Type: application/pgp-signature
 Content-Disposition: inline
 
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.2.4 (FreeBSD)
 
 iD8DBQFBChJGO4S8/gLNrjcRAodQAJ951qp8yxY5B6y8Fk6GFvykvbfECwCgmd67
 ovQTHPfFdMOHK43A+lOieY0=
 =2ciO
 -----END PGP SIGNATURE-----
 
 --9jxsPFA5p3P2qPhR--

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

 > > 2nd strtoul() will success, but errno == ERANGE.
 > > Is it necessary to set errno as 0 before calling 2nd strtoul() ?
 > Yes: a succesful library call leaves errno unchanged, rather than
 > setting it to 0 (usually).
 
 I understand. 
 Please close bug report as my mistake. 
 
 Thank you.
 
 ------
 takayuki@finet.fujitsu.com
State-Changed-From-To: open->closed 
State-Changed-By: maxim 
State-Changed-When: Fri Jul 30 10:12:00 GMT 2004 
State-Changed-Why:  
The submitter agrees this is not a bug. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=69788 
>Unformatted:
