strtold does not set errno when it should

Corinna Vinschen vinschen@redhat.com
Mon Dec 16 10:39:00 GMT 2019


Hi Bruno,

On Dec 16 11:05, Bruno Haible wrote:
> Hi Corinna,
> 
> > The code for strtold is almost verbatim taken from
> > 
> >   https://github.com/jwiegley/gdtoa.git
> > 
> > There haven't been any patches upstream.
> 
> Probably that's because the code is good enough for ISO C compliance;
> however, POSIX [1] adds the "ERANGE upon underflow" requirement,
> whereas ISO C only has an "ERANGE upon overflow" requirement.
> 
> [1] <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strtod.html>
> 
> > diff --git a/newlib/libc/stdlib/strtodg.c b/newlib/libc/stdlib/strtodg.c
> > index 013315946c1b..d6fb26ad3b45 100644
> > --- a/newlib/libc/stdlib/strtodg.c
> > +++ b/newlib/libc/stdlib/strtodg.c
> > @@ -1091,6 +1091,10 @@ _strtodg_l (struct _reent *p, const char *s00, char **se, FPI *fpi, Long *exp,
> >  				irv |= STRTOG_Underflow;
> >  			}
> >  		}
> > +#ifndef NO_ERRNO
> > +	if (irv & STRTOG_Underflow)
> > +		errno = ERANGE;
> > +#endif
> >  	if (se)
> >  		*se = (char *)s;
> >  	if (sign)
> > 
> > which seems to do the trick.  But, does it make sense?  If not, I'd
> > really appreciate a patch.
> 
> I think it goes in the right direction.
> 
> It can be improved a bit, though: The existing code for overflow is careful
> to do the errno stuff only when STRTOG_Overflow is being set/added. I.e.
> keep this overflow/underflow handling out of the main path, in order not
> to slow down the 99.99% of the cases that don't need it.
> 
> My attempt to do this would look like the attached patch. Untested.

That looks good to me and the result is the expected one.

If nobody complains in the next few hours, I'll check this in.
I *think* as of yet Cygwin is the only user of this code anyway.

Thanks!


Corinna

-- 
Corinna Vinschen
Cygwin Maintainer
Red Hat
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/newlib/attachments/20191216/9bc32c61/attachment.sig>


More information about the Newlib mailing list