bc: fix "...; return}" to work, disallow "return ()"
authorDenys Vlasenko <vda.linux@googlemail.com>
Thu, 3 Jan 2019 22:34:36 +0000 (23:34 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Thu, 3 Jan 2019 22:34:36 +0000 (23:34 +0100)
function                                             old     new   delta
zbc_parse_expr                                        24    1865   +1841
zbc_parse_stmt_possibly_auto                        1425    1413     -12
bc_parse_expr_empty_ok                              1843       -   -1843
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 1/1 up/down: 1841/-1855)        Total: -14 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
miscutils/bc.c
testsuite/bc.tests
testsuite/bc_misc1.bc

index e5726ca400bc0bfb28ad7408d6283bc80166c27a..6d8e2d991eacc9f40812b2bcde9f22b3b545d075 100644 (file)
@@ -4121,18 +4121,15 @@ static BC_STATUS zbc_parse_return(void)
        if (s) RETURN_STATUS(s);
 
        t = p->lex;
-       if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON)
+       if (t == XC_LEX_NLINE || t == BC_LEX_SCOLON || t == BC_LEX_RBRACE)
                xc_parse_push(BC_INST_RET0);
        else {
-               bool paren = (t == BC_LEX_LPAREN);
-               s = bc_parse_expr_empty_ok(0);
-               if (s == BC_STATUS_PARSE_EMPTY_EXP) {
-                       xc_parse_push(BC_INST_RET0);
-                       s = zxc_lex_next();
-               }
+               s = zbc_parse_expr(0);
                if (s) RETURN_STATUS(s);
 
-               if (!paren || p->lex_last != BC_LEX_RPAREN) {
+               if (t != BC_LEX_LPAREN   // "return EXPR", no ()
+                || p->lex_last != BC_LEX_RPAREN  // example: "return (a) + b"
+               ) {
                        s = zbc_POSIX_requires("parentheses around return expressions");
                        if (s) RETURN_STATUS(s);
                }
index 7795183a787ab0115af0c6049fb7488d12134ee1..0a8222be62b547b2967daa3973b3b68a4bb04cb8 100755 (executable)
@@ -103,6 +103,11 @@ testing "bc for (;;) {break}" \
        "2\n9\n" \
        "" "for (;;) {2;break}; 9"
 
+testing "bc define {return}" \
+       "bc" \
+       "0\n9\n" \
+       "" "define w() {return}\nw();9"
+
 testing "bc define auto" \
        "bc" \
        "8\n9\n" \
index 7e9d9660457f6adce9be3d82d39a5e3491928a4f..f666d701aa1bac635e033dece9f9bfaf90550635 100644 (file)
@@ -4,9 +4,7 @@ define x(x) {
 define y() {
        return;
 }
-define z() {
-       return ();
-}
+define z() {return}
 scale = 0
 x=2
 x[0]=3