Source-Changes-HG archive

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

[src/trunk]: src/bin/sh In uses like $(( var )) (un-dollared var...



details:   https://anonhg.NetBSD.org/src/rev/cddc1711a983
branches:  trunk
changeset: 318412:cddc1711a983
user:      kre <kre%NetBSD.org@localhost>
date:      Sat Apr 21 23:01:29 2018 +0000
description:
In uses like $(( var )) (un-dollared vars in arithmetic) we allow
leading whitespace in the value of var (because strtoimax() does)
but did not allow trailing whitespace.   The effect is that some
cases where $(( ${var:-0} )) would work do not work without the $
expansion.

Fix that - allow trailing whitespace.   However, continue to insist
upon at least one digit (a non-null var that contains nothing but
whitespace is still an error).

Note: posix is not helpful here, it simply requires that the variable
contain "a value that forms a valid integer constant" (with an optional
+ or - sign).

diffstat:

 bin/sh/arithmetic.c |  14 +++++++++++---
 1 files changed, 11 insertions(+), 3 deletions(-)

diffs (43 lines):

diff -r baa4166fce74 -r cddc1711a983 bin/sh/arithmetic.c
--- a/bin/sh/arithmetic.c       Sat Apr 21 21:32:14 2018 +0000
+++ b/bin/sh/arithmetic.c       Sat Apr 21 23:01:29 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: arithmetic.c,v 1.4 2017/07/24 13:21:14 kre Exp $       */
+/*     $NetBSD: arithmetic.c,v 1.5 2018/04/21 23:01:29 kre Exp $       */
 
 /*-
  * Copyright (c) 1993
@@ -39,7 +39,7 @@
 #include <sys/cdefs.h>
 
 #ifndef lint
-__RCSID("$NetBSD: arithmetic.c,v 1.4 2017/07/24 13:21:14 kre Exp $");
+__RCSID("$NetBSD: arithmetic.c,v 1.5 2018/04/21 23:01:29 kre Exp $");
 #endif /* not lint */
 
 #include <limits.h>
@@ -59,6 +59,7 @@
 #include "options.h"
 #include "var.h"
 #include "show.h"
+#include "syntax.h"
 
 #if ARITH_BOR + ARITH_ASS_GAP != ARITH_BORASS || \
        ARITH_ASS + ARITH_ASS_GAP != ARITH_EQ
@@ -127,8 +128,15 @@
                str = "0";
        errno = 0;
        result = strtoimax(str, &p, 0);
-       if (errno != 0 || *p != '\0')
+       if (errno != 0 || *p != '\0') {
+               if (errno == 0) {
+                       while (*p != '\0' && is_space(*p))
+                               p++;
+                       if (*p == '\0')
+                               return result;
+               }
                arith_err("variable contains non-numeric value");
+       }
        return result;
 }
 



Home | Main Index | Thread Index | Old Index