dc: execute shouldn't pop if stack head is not a string
authorBrian Foley <bpfoley@google.com>
Thu, 5 Sep 2019 08:46:22 +0000 (10:46 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Thu, 5 Sep 2019 08:46:22 +0000 (10:46 +0200)
This matches the behaviour of both GNU dc (as specified in
its man page), and BSD dc (where stack_popstring() pops
only if the head is a string.)

Add a couple of tests to verify this behavior.

function                                             old     new   delta
zxc_vm_process                                      6882    6884      +2

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

index 7ac30dd532fb109aea2221e67099898954649917..4a3ae49c5717051f531b851136cca77addd012ed 100644 (file)
@@ -6521,7 +6521,7 @@ static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
                        if (s || !BC_PROG_STR(n)) goto exit;
                        sidx = n->rdx;
                } else
-                       goto exit;
+                       goto exit_nopop;
        }
 
        fidx = sidx + BC_PROG_REQ_FUNCS;
@@ -6561,6 +6561,7 @@ static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
        RETURN_STATUS(BC_STATUS_SUCCESS);
  exit:
        bc_vec_pop(&G.prog.results);
+ exit_nopop:
        RETURN_STATUS(s);
 }
 #define zdc_program_execStr(...) (zdc_program_execStr(__VA_ARGS__) COMMA_SUCCESS)
index 8c3af415671685ca94f882dc1949d9468ab89a5a..f74e3f0f5c317c7d5aeef99caa6a9fdc598e4445 100755 (executable)
@@ -44,6 +44,16 @@ testing "dc complex without spaces (multiple args)" \
 optional FEATURE_DC_BIG
 # All tests below depend on FEATURE_DC_BIG
 
+testing "dc: x should execute strings" \
+       "dc -e'[40 2 +] x f'" \
+       "42\n" \
+       "" ""
+
+testing "dc: x should not execute or pop non-strings" \
+       "dc -e'42 x f'" \
+       "42\n" \
+       "" ""
+
 testing "dc read" \
        "dc -finput" \
        "2\n9\n1\n" \