ash: fix BASE###nn bashism to accept letter 'digits' for bases > 9
authorDenys Vlasenko <vda.linux@googlemail.com>
Sun, 22 Sep 2019 16:26:05 +0000 (18:26 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 22 Sep 2019 16:26:05 +0000 (18:26 +0200)
function                                             old     new   delta
evaluate_string                                      873     876      +3

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/math.c

index eaf4f245320e921b7cd6f7ced2185d53842c3d2b..0c806ad39107dcace3eed5f5e8cdc78a0092ae22 100644 (file)
@@ -538,9 +538,16 @@ static arith_t strto_arith_t(const char *nptr, char **endptr)
        n = 0;
        nptr = *endptr + 1;
        /* bash allows "N#" (empty "nnnn" part) */
-       while (isdigit(*nptr)) {
+       for (;;) {
+               unsigned digit = (unsigned)*nptr - '0';
+               if (digit >= 10 || digit >= base) {
+                       digit = (unsigned)(*nptr | 0x20) - ('a' - 10);
+                       if (digit >= base)
+                               break;
+               }
                /* bash does not check for overflows */
-               n = n * base + (*nptr++ - '0');
+               n = n * base + digit;
+               nptr++;
        }
        *endptr = (char*)nptr;
        return n;