bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] Overflow to Infinity


From: Andrew J. Schorr
Subject: Re: [bug-gawk] Overflow to Infinity
Date: Tue, 19 Jun 2018 08:47:46 -0400
User-agent: Mutt/1.5.21 (2010-09-15)

Hi,

On Tue, Jun 19, 2018 at 05:06:01AM -0700, Daniel Pettet wrote:
> The following test program illustrates a problem with overflow of large
> numbers to what should be infinity.

What's going on here is that gawk's node.c:r_force_number function is calling
strtod to convert the number and then noticing that strtod has set errno to
ERANGE. Then we have this comment in the code:

                /*
                 * N.B. For subnormal values, strtod may return the
                 * floating-point representation while setting errno to ERANGE.
                 * We force the numeric value to 0 in such cases.
                 */
                n->numbr = 0;
                /*
                 * Or should we accept it as a NUMBER even though strtod
                 * threw an error?
                 */
                /* fall through to badnum */

So gawk sets the numeric value to zero and decides that the input is
actually a string, not a number.

I'm not sure about the language lawyering on this topic. Perhaps one could
argue that an exception should be made for ERANGE.

With this patch, gawk does what you want (on my Linux system):

diff --git a/node.c b/node.c
index fcd2bf3..b879e64 100644
--- a/node.c
+++ b/node.c
@@ -140,7 +140,7 @@ r_force_number(NODE *n)
                *cpend = save;
        }
 
-       if (errno == 0) {
+       if (errno == 0 || errno == ERANGE){
                if (ptr == cpend)
                        goto goodnum;
                /* else keep the leading numeric value without updating flags */


It also passes "make check", somewhat to my surprise.

Note: I'm not sure whether this patch is ideal, since it neglects to
reset errno to zero, as happens in the "else" clause. I don't know whether
that errno reset is actually needed...

Regards,
Andy



reply via email to

[Prev in Thread] Current Thread [Next in Thread]