[PATCH] awk: Fix overly permissive func arg list parsing

Brian Foley bpfoley at google.com
Mon Jan 7 02:32:59 UTC 2019


It allows things like 'func f(a b)' and 'func f(a,)' which GNU awk
forbids.

Signed-off-by: Brian Foley <bpfoley at google.com>
---
 editors/awk.c       | 17 +++++++++++++++--
 testsuite/awk.tests | 12 ++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/editors/awk.c b/editors/awk.c
index 0450adc77..69363d29f 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -1662,12 +1662,25 @@ static void parse_program(char *p)
 			f = newfunc(t_string);
 			f->body.first = NULL;
 			f->nargs = 0;
-			while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
+			/* Match func arg list: a comma sep list of >= 0 args, and a close paren */
+			while (next_token(TC_VARIABLE | TC_SEQTERM | TC_COMMA)) {
+				/* Either an empty arg list, or trailing comma from prev iter
+				 * must be followed by an arg */
+				if (f->nargs == 0 && t_tclass == TC_SEQTERM)
+					break;
+
+				/* TC_SEQSTART/TC_COMMA must be followed by TC_VARIABLE */
+				if (t_tclass != TC_VARIABLE)
+					syntax_error(EMSG_UNEXP_TOKEN);
+
 				v = findvar(ahash, t_string);
 				v->x.aidx = f->nargs++;
 
-				if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
+				/* Arg followed either by end of arg list or 1 comma */
+                if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
 					break;
+                if (t_tclass != TC_COMMA)
+					syntax_error(EMSG_UNEXP_TOKEN);
 			}
 			seq = &f->body;
 			chain_group();
diff --git a/testsuite/awk.tests b/testsuite/awk.tests
index 03fedf771..725d287c2 100755
--- a/testsuite/awk.tests
+++ b/testsuite/awk.tests
@@ -280,6 +280,18 @@ testing "awk 'delete a[v--]' evaluates v-- once" \
 " \
 	"" ""
 
+testing "awk func arg parsing 1" \
+	"awk 'func f(,) { }' 2>&1" "awk: cmd. line:1: Unexpected token" "" ""
+
+testing "awk func arg parsing 2" \
+	"awk 'func f(a,,b) { }' 2>&1" "awk: cmd. line:1: Unexpected token" "" ""
+
+testing "awk func arg parsing 3" \
+	"awk 'func f(a,) { }' 2>&1" "awk: cmd. line:1: Unexpected token" "" ""
+
+testing "awk func arg parsing 4" \
+	"awk 'func f(a b) { }' 2>&1" "awk: cmd. line:1: Unexpected token" "" ""
+
 testing "awk handles empty ()" \
 	"awk 'BEGIN {print()}' 2>&1" "awk: cmd. line:1: Empty sequence\n" "" ""
 
-- 
2.17.1



More information about the busybox mailing list