dc: Fix segfault when executing strings generated using asciify
authorBrian Foley <bpfoley@google.com>
Thu, 5 Sep 2019 08:50:13 +0000 (10:50 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Mon, 21 Oct 2019 14:54:40 +0000 (16:54 +0200)
function                                             old     new   delta
zxc_vm_process                                      6884    6891      +7

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

index 9a9880706f6673ef33cf38a8c51e38c5a533c846..44c70cac36ff4f519170f0e5dc01660dd9992bb6 100644 (file)
@@ -4973,7 +4973,9 @@ static void dc_parse_string(void)
        xc_parse_pushInst_and_Index(XC_INST_STR, len);
        bc_vec_push(&G.prog.strs, &str);
 
-       // Explanation needed here
+       // Add an empty function so that if zdc_program_execStr ever needs to
+       // parse the string into code (from the 'x' command) there's somewhere
+       // to store the bytecode.
        xc_program_add_fn();
        p->func = xc_program_func(p->fidx);
 
@@ -6398,7 +6400,11 @@ static BC_STATUS zdc_program_asciify(void)
        str = xzalloc(2);
        str[0] = c;
        //str[1] = '\0'; - already is
-       bc_vec_push(&G.prog.strs, &str);
+       idx = bc_vec_push(&G.prog.strs, &str);
+       // Add an empty function so that if zdc_program_execStr ever needs to
+       // parse the string into code (from the 'x' command) there's somewhere
+       // to store the bytecode.
+       xc_program_add_fn();
  dup:
        res.t = XC_RESULT_STR;
        res.d.id.idx = idx;
index f74e3f0f5c317c7d5aeef99caa6a9fdc598e4445..1fc13c2018db1211bb772da1fde96007a118b7cc 100755 (executable)
@@ -54,6 +54,11 @@ testing "dc: x should not execute or pop non-strings" \
        "42\n" \
        "" ""
 
+testing "dc: x should work with strings created from a" \
+       "dc -e'42 112 a x'" \
+       "42\n" \
+       "" ""
+
 testing "dc read" \
        "dc -finput" \
        "2\n9\n1\n" \