The test-test example in the comment wasn't actually fixed before as
one can see by running
        env -i "test-test=test" busybox ash -c 'export -p'

This previously incorrectly resulted in
        export test

But now it correctly results in
        export 'test-test=test'

function                                             old     new   delta
setvar                                               201     204      +3
static.showvars                                      211     207      -4
.rodata                                           100410  100406      -4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 3/-8)               Total: -5 bytes
---
 shell/ash.c | 29 ++++++++++++-----------------
 1 file changed, 12 insertions(+), 17 deletions(-)

diff --git a/shell/ash.c b/shell/ash.c
index 9173b8608..fd21afdb2 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -2495,7 +2495,7 @@ setvar(const char *name, const char *val, int flags)
        q = endofname(name);
        p = strchrnul(q, '=');
        namelen = p - name;
-       if (!namelen || p != q)
+       if (!namelen || (~flags & VEXPORT && p != q))
                ash_msg_and_raise_error("%.*s: bad variable name", namelen, 
name);
        vallen = 0;
        if (val == NULL) {
@@ -11573,31 +11573,26 @@ shiftcmd(int argc UNUSED_PARAM, char **argv)
 static int
 showvars(const char *sep_prefix, int on, int off)
 {
-       const char *sep;
        char **ep, **epend;
 
        ep = listvars(on, off, /*strlist:*/ NULL, &epend);
        qsort(ep, epend - ep, sizeof(char *), vpcmp);
 
-       sep = *sep_prefix ? " " : sep_prefix;
-
        for (; ep < epend; ep++) {
-               const char *p;
                const char *q;
 
-               p = endofname(*ep);
-/* Used to have simple "p = strchrnul(*ep, '=')" here instead, but this
- * makes "export -p" to have output not suitable for "eval":
- * import os
- * os.environ["test-test"]="test"
- * if os.fork() == 0:
- *   os.execv("ash", [ 'ash', '-c', 'eval $(export -p); echo OK' ])  # fixes 
this
- * os.execv("ash", [ 'ash', '-c', 'env | grep test-test' ])
- */
-               q = nullstr;
-               if (*p == '=')
+               if (*sep_prefix == '\0') {
+                       const char *p = endofname(*ep);
+                       if (*p != '=')
+                               // Parsing would fail so ignore the variable.
+                               // This is explicitly allowed by POSIX.
+                               continue;
                        q = single_quote(++p);
-               out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, 
q);
+                       out1fmt("%.*s%s\n", (int)(p - *ep), *ep, q);
+               } else {
+                       q = single_quote(*ep);
+                       out1fmt("%s %s\n", sep_prefix, q);
+               }
        }
        return 0;
 }
-- 
2.48.1

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

Reply via email to