awk: fix segfault on for loop syntax error
authorBrian Foley <bpfoley@google.com>
Sat, 15 Oct 2016 13:45:40 +0000 (14:45 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Mon, 24 Oct 2016 00:09:56 +0000 (02:09 +0200)
Parsing "for()" segfaults as awk fails to find loop iteration expressions.

Signed-off-by: Brian Foley <bpfoley@google.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
editors/awk.c
testsuite/awk.tests

index d0269b9f43bd340d2eb5ce7fe6625fe7b9ca1ce8..685e8bed83fab87ffa603f2e740aec7ce6e8dde9 100644 (file)
@@ -1514,7 +1514,7 @@ static void chain_group(void)
                        next_token(TC_SEQSTART);
                        n2 = parse_expr(TC_SEMICOL | TC_SEQTERM);
                        if (t_tclass & TC_SEQTERM) {    /* for-in */
-                               if ((n2->info & OPCLSMASK) != OC_IN)
+                               if (!n2 || (n2->info & OPCLSMASK) != OC_IN)
                                        syntax_error(EMSG_UNEXP_TOKEN);
                                n = chain_node(OC_WALKINIT | VV);
                                n->l.n = n2->l.n;
index adab4ae1ec2963dde24f7c7846dfd0b905935133..82937bc10b41bf907573fe8d87a31212706b8d82 100755 (executable)
@@ -316,6 +316,9 @@ testing "awk continue" \
        "" \
        'BEGIN { if (1) continue; else a = 1 }'
 
+testing "awk handles invalid for loop" \
+    "awk '{ for() }' 2>&1" "awk: cmd. line:1: Unexpected token\n" "" ""
+
 # testing "description" "command" "result" "infile" "stdin"
 
 exit $FAILCOUNT