bc: fix "echo -n '#foo' | bc" not eating last 'o'
authorDenys Vlasenko <vda.linux@googlemail.com>
Tue, 18 Dec 2018 13:37:16 +0000 (14:37 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Tue, 18 Dec 2018 13:39:33 +0000 (14:39 +0100)
function                                             old     new   delta
zdc_parse_expr                                       656     653      -3
bc_lex_lineComment                                    39      36      -3
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-6)               Total: -6 bytes
   text    data     bss     dec     hex filename
 981424     485    7296  989205   f1815 busybox_old
 981418     485    7296  989199   f180f busybox_unstripped

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

index a5fcaf3bcafaa8702028f0d81f39325ebf90f956..eaa28a94a4e8046fe9ab5be10cdcc9c4d06fcddd 100644 (file)
@@ -2655,9 +2655,13 @@ static FAST_FUNC void bc_result_free(void *result)
 
 static void bc_lex_lineComment(BcLex *l)
 {
+       // Try: echo -n '#foo' | bc
+       size_t i;
        l->t.t = BC_LEX_WHITESPACE;
-       while (l->i < l->len && l->buf[l->i++] != '\n');
-       --l->i;
+       i = l->i;
+       while (i < l->len && l->buf[i] != '\n')
+               i++;
+       l->i = i;
 }
 
 static void bc_lex_whitespace(BcLex *l)
@@ -2889,8 +2893,8 @@ static BC_STATUS zbc_lex_next(BcLex *l)
        // Comments are also BC_LEX_WHITESPACE tokens and eaten here.
        s = BC_STATUS_SUCCESS;
        do {
-               l->t.t = BC_LEX_EOF;
                if (l->i == l->len) {
+                       l->t.t = BC_LEX_EOF;
                        if (!G.input_fp)
                                RETURN_STATUS(BC_STATUS_SUCCESS);
                        if (!bc_lex_more_input(l)) {
index e0a45a8bd9042c67f1a04cb11108bf9d0beec003..e303cf6ae2de144bae71503bf7b8af568aedadf5 100755 (executable)
@@ -16,6 +16,11 @@ testing "bc comment 2: /*/ is not a closed comment" \
        "4\n" \
        "" "1 /*/ + 2 */ + 3"
 
+testing "bc comment 3: unterminated #comment" \
+       "bc" \
+       "" \
+       "" "#foo"  # no trailing newline
+
 testing "bc backslash 1" \
        "bc" \
        "3\n" \