bc: for(;;) fix from upstream
authorDenys Vlasenko <vda.linux@googlemail.com>
Thu, 20 Dec 2018 23:35:22 +0000 (00:35 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Thu, 20 Dec 2018 23:35:22 +0000 (00:35 +0100)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
miscutils/bc.c
testsuite/bc.tests

index d37417f11359f2156e3fcf1eb49154b08f924285..e7983f38df074701a333491991bdc7975af721b0 100644 (file)
@@ -4205,8 +4205,14 @@ static BC_STATUS zbc_parse_for(BcParse *p)
 
        if (p->l.t.t != BC_LEX_SCOLON)
                s = zbc_parse_expr(p, BC_PARSE_REL);
-       else
+       else {
+               // Set this for the next call to bc_parse_number.
+               // This is safe to set because the current token is a semicolon,
+               // which has no string requirement.
+               bc_vec_string(&p->l.t.v, 1, "1");
+               bc_parse_pushNUM(p);
                s = bc_POSIX_does_not_allow_empty_X_expression_in_for("condition");
+       }
 
        if (s) RETURN_STATUS(s);
        if (p->l.t.t != BC_LEX_SCOLON) RETURN_STATUS(bc_error_bad_token());
index d33f8c90ae14e7562c272c83b2b95874a084746a..edff209bbdf3fb644e836b47a3f169a342239035 100755 (executable)
@@ -51,6 +51,11 @@ testing "bc if 0 else if 1" \
        "2\n9\n" \
        "" "if (0) 1 else if (1) 2; 9"
 
+testing "bc for(;;)" \
+       "bc" \
+       "2\n3\n2\n9\n" \
+       "" "i=2; for (;;) { 2; if(--i==0) break; 3; }; 9"
+
 testing "bc define auto" \
        "bc" \
        "8\n9\n" \