unix/sys_float.c mishandles errno

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

unix/sys_float.c mishandles errno

Matthew Dempsky-3
The code in unix/sys_float.c assumes that if strtod(3) sets errno to
ERANGE then there was a problem parsing the value.  However, according
to POSIX, the actual indication of an error is that it also returned
0.0, HUGE_VAL, or -HUGE_VAL.  This causes a problem on OS X 10.5 when
trying to read denormalized floating-point numbers because OS X's
strtod(3) implementation sets errno to ERANGE for denormals.

The patch below changes the errno check to be consistent with POSIX.

--- sys_float.c.orig    2008-01-02 10:38:16.000000000 -0800
+++ sys_float.c 2008-01-02 10:39:55.000000000 -0800
@@ -705,9 +705,7 @@
     if (*s)                    /* That should be it */
       return -1;

-#ifdef NO_FPE_SIGNALS
     errno = 0;
-#endif
     __ERTS_FP_CHECK_INIT(fpexnp);
     *fp = strtod(buf, &t);
     __ERTS_FP_ERROR_THOROUGH(fpexnp, *fp, return -1);
@@ -720,14 +718,14 @@
        __ERTS_FP_ERROR_THOROUGH(fpexnp, *fp, return -1);
     }

+    if ((*fp == 0.0 || *fp == HUGE_VAL || *fp == -HUGE_VAL) && errno
== ERANGE) {
 #ifdef DEBUG
-    if (errno == ERANGE)
        fprintf(stderr, "errno = ERANGE in list_to_float\n\r");
 #endif
 #ifdef NO_FPE_SIGNALS
-    if (errno == ERANGE)
        return -1;
 #endif
+    }
 /*
 **  Replaces following code:
 **   if (errno == ERANGE) {


Reply | Threaded
Open this post in threaded view
|

unix/sys_float.c mishandles errno

Björn Gustavsson-3
"Matthew Dempsky" <matthew> writes:

> The code in unix/sys_float.c assumes that if strtod(3) sets errno to
> ERANGE then there was a problem parsing the value.  However, according
> to POSIX, the actual indication of an error is that it also returned
> 0.0, HUGE_VAL, or -HUGE_VAL.  This causes a problem on OS X 10.5 when
> trying to read denormalized floating-point numbers because OS X's
> strtod(3) implementation sets errno to ERANGE for denormals.
>
> The patch below changes the errno check to be consistent with POSIX.

Thanks!

We will test the patch in our daily build and include it in R12B-1
if we'll encounter no problems.

/Bjorn
--
Bj?rn Gustavsson, Erlang/OTP, Ericsson AB