From f6ab9a281dde719e180809db52d0d142d0c0a240 Mon Sep 17 00:00:00 2001 From: Peter Stephenson Date: Mon, 3 Apr 2017 10:03:13 +0100 Subject: 40932: Parameter subscripts need to count parentheses. Otherwise they can terminate in the middle of an expression. --- ChangeLog | 5 +++++ Src/params.c | 11 ++++++++--- Test/D06subscript.ztst | 7 +++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 892d1f25e..221e3dcb9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2017-04-03 Peter Stephenson + + * 40932: Src/params.c, Test/D06subscript.ztst: parameter + subscripts need to count parentheses to avoid terminating early. + 2017-04-02 Barton E. Schaefer * 40929 (replaces 40598): Src/subst.c: paramsubst() should always diff --git a/Src/params.c b/Src/params.c index 785b9ead7..a9683a6f6 100644 --- a/Src/params.c +++ b/Src/params.c @@ -1183,7 +1183,7 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w, int *prevcharlen, int *nextcharlen) { int hasbeg = 0, word = 0, rev = 0, ind = 0, down = 0, l, i, ishash; - int keymatch = 0, needtok = 0, arglen, len; + int keymatch = 0, needtok = 0, arglen, len, inpar = 0; char *s = *str, *sep = NULL, *t, sav, *d, **ta, **p, *tt, c; zlong num = 1, beg = 0, r = 0, quote_arg = 0; Patprog pprog = NULL; @@ -1322,8 +1322,9 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w, } for (t = s, i = 0; - (c = *t) && ((c != Outbrack && - (ishash || c != ',')) || i); t++) { + (c = *t) && + ((c != Outbrack && (ishash || c != ',')) || i || inpar); + t++) { /* Untokenize inull() except before brackets and double-quotes */ if (inull(c)) { c = t[1]; @@ -1344,6 +1345,10 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w, i++; else if (c == ']' || c == Outbrack) i--; + if (c == '(' || c == Inpar) + inpar++; + else if (c == ')' || c == Outpar) + inpar--; if (ispecial(c)) needtok = 1; } diff --git a/Test/D06subscript.ztst b/Test/D06subscript.ztst index 144923667..f0a858b1c 100644 --- a/Test/D06subscript.ztst +++ b/Test/D06subscript.ztst @@ -266,3 +266,10 @@ >of the gang >of the gang >of the gang + + string='abcde' + twoarg() { return $(( $2 - $1 )) } + functions -M twoarg + print ${string[1,twoarg(1,4)]} +0:Commas inside parentheses do not confuse subscripts +>abc -- cgit 1.4.1