bug-gawk
[Top][All Lists]
Advanced

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

Re: gawk doesn't treat '018' as decimal 18


From: arnold
Subject: Re: gawk doesn't treat '018' as decimal 18
Date: Sat, 09 Jan 2021 13:21:41 -0700
User-agent: Heirloom mailx 12.5 7/5/10

Hi.

Thanks for the reports, both of you. This was two separate bugs,
one for doubles and one for MPFR.  The patch below fixes both.

It'll be pushed to Git shortly.

Thanks,

Arnold

Jean-Philippe Guérard via "Bug reports and all discussion about gawk." 
<bug-gawk@gnu.org> wrote:

> Le Sat, 9 Jan 2021 17:44:53 +0100, Arkadiusz Drabczyk a écrit :
> > In gawk.info it says:
> > 
> > "Unlike in some early C implementations, '8' and '9' are not valid in
> > octal constants.  For example, 'gawk' treats '018' as decimal 18:
> > 
> >      $ gawk 'BEGIN { print "021 is", 021 ; print 018 }'
> >      -| 021 is 17
> >      -| 18
> > "
> > 
> > but I cannot reproduce it wit gawk built from today's master:
> > 
> > $ ./gawk 'BEGIN { print "021 is", 021 ; print 018 }'
> > 021 is 17
> > 0
>
> As an additional curiosity, with today's master and the bignum option, I
> get:
>
> $ gawk -M 'BEGIN { print "021 is", 021 ; print 018 }'
> 021 is 17
> 1
>
> Which is kind of logical (stops at the first non octal digit), but
> also surprising.
>
> -- 
> Jean-Philippe Guérard
> https://tigrerayé.org
>
-------------------------
diff --git a/ChangeLog b/ChangeLog
index 60fe7772..56b6a44a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2021-01-09         Arnold D. Robbins     <arnold@skeeve.com>
+
+       Fix problems turning something like 018 into decimal.
+       Thanks to Arkadiusz Drabczyk <arkadiusz@drabczyk.org> for the report.
+
+       * builtin.c (nondec2awknum): Use a copy of len, in case we detect
+       8 or 9 and have to restart as decimal.
+       * mpfr.c (mpg_strtoui): For 8 or 9, set base to 10.
+
 2021-01-08         Arnold D. Robbins     <arnold@skeeve.com>
 
        General tightening up use of const and types. Thanks to
diff --git a/builtin.c b/builtin.c
index fc5dc31e..e28c35ef 100644
--- a/builtin.c
+++ b/builtin.c
@@ -3,7 +3,7 @@
  */
 
 /*
- * Copyright (C) 1986, 1988, 1989, 1991-2020,
+ * Copyright (C) 1986, 1988, 1989, 1991-2021,
  * the Free Software Foundation, Inc.
  *
  * This file is part of GAWK, the GNU implementation of the
@@ -3684,7 +3684,9 @@ nondec2awknum(char *str, size_t len, char **endptr)
                if (endptr)
                        *endptr = str;
        } else if (len >= 1 && *str == '0') {
-               for (; len > 0; len--) {
+               int l;
+               // preserve len in case we go to decimal
+               for (l = len; l > 0; l--) {
                        if (! isdigit((unsigned char) *str)) {
                                if (endptr)
                                        *endptr = str;
diff --git a/mpfr.c b/mpfr.c
index cf53bbdd..cabc3910 100644
--- a/mpfr.c
+++ b/mpfr.c
@@ -3,7 +3,7 @@
  */
 
 /*
- * Copyright (C) 2012, 2013, 2015, 2017, 2018, 2019,
+ * Copyright (C) 2012, 2013, 2015, 2017, 2018, 2019, 2021
  * the Free Software Foundation, Inc.
  *
  * This file is part of GAWK, the GNU implementation of the
@@ -176,7 +176,7 @@ mpg_strtoui(mpz_ptr zi, char *str, size_t len, char **end, 
int base)
                case '8':
                case '9':
                        if (base == 8)
-                               goto done;
+                               base = 10;
                        break;
                case 'a':
                case 'b':



reply via email to

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