Re: strtold does not set errno when it should

Corinna Vinschen <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
Hi Bruno,

[moving this discussion to the newlib list since that's newlib code]

On Dec 12 07:38, Bruno Haible wrote:
> POSIX [1] makes it clear that when the value to be returned would cause
> underflow, it should set errno to ERANGE.
> 
> [1] <https://pubs.opengroup.org/onlinepubs/9699919799/functions/strtod.html>
> 
> This test case fails with error code 4 on Cygwin 2.9.
> 
> ========================== foo.c ==========================
> #include <stdlib.h>
> #include <errno.h>
> #include <float.h>
> #include <math.h>
> 
> int main ()
> {
>   const char input[] = "1E-100000";
>   char *ptr;
>   long double result;
>   errno = 0;
>   result = strtold (input, &ptr);
>   if (!(ptr == input + 9))
>     return 1;
>   if (!(0.0L <= result && result <= LDBL_MIN))
>     return 2;
>   if (signbit (result))
>     return 3;
>   if (result == 0.0L && errno != ERANGE)
>     return 4;
>   return 0;
> }
> ============================================================
> $ gcc -Wall foo.c
> $ ./a.exe
> $ echo $?
> 4
> 
> The corresponding test case for strtod() / 'double' succeeds.

The code for strtold is almost verbatim taken from

  https://github.com/jwiegley/gdtoa.git

There haven't been any patches upstream.  I, for one, am not overly
fluent with FP math, so I'm glad for any help.

Looking into _strtodg_l (newlib/libc/stdlib/strtodg.c) I tried this
patch:

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.


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEEoVYPmneWZnwT6kwF9TYGna5ET6AFAl33SicACgkQ9TYGna5E
T6CTig/5ATSeip9xSQ4Ktzj2gx+52eWkfuHP1zNkRR0KtL8pVGX99qgrUJ8mYh0h
b+hIkmjWOEAE9o+jaBsq/d68CnIQpzt8gZJ+rM0ahKspc++UNQe31S99xp77LhVV
uIeelSSRWX4kRBx+Jmv5b8z/j9Ls2yw1hebz62rGVO8uolRL/0KRSE3fyghVB8q9
FDcp4k67cBgf9dyVe7WYJW2MAn4XmWr99x58bp0XO7X5ndthVZZm/2U97J0JksJ0
MQ/cYZgHyDBMfvsg8+5eKSA3DScLeA8gRWc+WEkrLh4jbcAjL2Ssb0LnoEOwoStF
NcdaPdCa13vTrO1SBS8RMLOZI/lrB1lcKQk0j0mqQpKfYR6nQ7N16L7lF6lKtIoN
SQeiZnulkny1ZnLjJ+LvQPnRCthYPBWealCmjx2yHxCMVY0dLO+FJJoAz3soqRhZ
IV/M1abdj6HL+w1NwIPxqf+a1wprf+JhepFPIgiOOOwP6o0wsZsoxOjRHU1KHpu2
rfhDuNzyju9R0VfWhQMwpAJMQd/ItoJxWbHFYgN+RjjEYztDaJCre56G09OnjK5x
Uk0a4qMpbOidor1EIapgjdVxWX57D/AkqF1hrPlBuDUzSv7boCsh9Hv1FJOco65+
z+7X8tNjer7J0Dt67dvi6WALePYNYcWLqzoplL61N1zHUNhriuY=
=NWhD
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.