delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2004/02/27/13:03:41

X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
Date: 25 Feb 2004 08:06:09 +0200
Message-Id: <uvflvelri.fsf@elta.co.il>
From: Eli Zaretskii <eliz AT elta DOT co DOT il>
To: Eric Rudd <rudd AT cyberoptics DOT com>
CC: djgpp-workers AT delorie DOT com
In-reply-to: <403BD3E2.90602@cyberoptics.com> (message from Eric Rudd on Tue,
24 Feb 2004 16:44:50 -0600)
Subject: Re: strtoul bug (was Re: Fibonacci number)
References: <d7c3a0b2 DOT 0402220759 DOT 34d6435d AT posting DOT google DOT com> <4038E8CA DOT 6491815E AT virginia DOT edu> <4039DD96 DOT 3F36F3B7 AT yahoo DOT com> <200402231458 DOT i1NEwKwm020904 AT envy DOT delorie DOT com> <403A301C DOT E8F5FE65 AT yahoo DOT com> <200402231751 DOT i1NHp5lv022894 AT envy DOT delorie DOT com> <403A5F55 DOT 7E608910 AT yahoo DOT com> <200402232034 DOT i1NKYqrt024366 AT envy DOT delorie DOT com> <403ADB4A DOT ED9F77C2 AT yahoo DOT com> <403BD3E2 DOT 90602 AT cyberoptics DOT com>
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

> Date: Tue, 24 Feb 2004 16:44:50 -0600
> From: Eric Rudd <rudd AT cyberoptics DOT com>
> 
> For me, the question whose answer is most unclear is about the proper 
> thing to do when the string represents a number that is out of range.  
>  From a reading of 7.20.1.4, it appears to me that ULONG_MAX should be 
> returned, regardless of whether the number was out of range in the 
> positive or negative direction, and regardless of how far, but I'm by no 
> means sure of that.  Thus, the present strtoul() is correct for an input 
> string of "-1", but it appears to treat other negative constants as if 
> they were first converted to a signed long and then to an unsigned long, 
> which I think is incorrect.

I'm not sure the current behavior is wrong.  A case in point is a
line like below from a somewhat buggy C program:

     unsigned foo = -5;

What do we expect a typical C compiler to produce?  I think we expect
it to treat this as

     unsigned foo = (unsigned)-5;

which produces what the current strtoul does.  The reason this is
relevant is that a compiler (or any other similar processor) could
use strtoul to convert the string to an unsigned value.

Another case in point is

	int bar = strtoul("-5", NULL, 0);

Don't we expect `bar' to be assigned the value -5?

So in my view, setting errno is okay, but the value should not
blindly be ULONG_MAX.  I think ULONG_MAX is for the cases where we
try to convert a string whose numerical value cannot be reasonably
represented as a 32-bit number, like "11111111111111111111111111111"
or some such.

FWIW, the GNU/Linux documentation of strtoul explicitly says that
ULONG_MAX is returned and errno is set only if the original value
would overflow.  Curiously, it also says:

       The  strtoul()  function  returns either the result of the
       conversion or, if there was  a  leading  minus  sign,  the
       negation of the result of the conversion

Does this mean it will return 5 in the last example above?  Can
someone try this on a GNU/Linux box?

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019