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.
---
 miscutils/bc.c     |  3 ++-
 testsuite/dc.tests | 10 ++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/miscutils/bc.c b/miscutils/bc.c
index 75ffab460..82425b39c 100644
--- a/miscutils/bc.c
+++ b/miscutils/bc.c
@@ -6522,7 +6522,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;
@@ -6562,6 +6562,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)
diff --git a/testsuite/dc.tests b/testsuite/dc.tests
index 8c3af4156..89c5c2406 100755
--- a/testsuite/dc.tests
+++ b/testsuite/dc.tests
@@ -41,6 +41,16 @@ testing "dc complex without spaces (multiple args)" \
        "16\n" \
        "" ""
 
+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" \
+       "" ""
+
 optional FEATURE_DC_BIG
 # All tests below depend on FEATURE_DC_BIG
 
-- 
2.17.1

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to