[Toybox] [PATCH] env: fix case where a variable is replaced.

enh enh at google.com
Thu May 2 18:47:51 PDT 2019


Found when trying to update the toybox prebuilt used for the Android
build.

Also add the corresponding test.
---
 lib/env.c      | 4 +---
 tests/env.test | 2 ++
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/lib/env.c b/lib/env.c
index 35ef688c..bc23b753 100644
--- a/lib/env.c
+++ b/lib/env.c
@@ -58,11 +58,9 @@ void xsetenv(char *name, char *val)
     if (!memcmp(name, environ[i], len) && environ[i][len]=='=') {
       if (i>=envc) free(environ[i]);
       else {
-        char **delete = environ+i;
-
         // move old entries down, add at end of old data
         toys.envc = envc--;
-        for (i=0; new ? i<envc : !!delete[i]; i++) delete[i] = delete[i+1];
+        for (; new ? i<envc : !!environ[i]; i++) environ[i] = environ[i+1];
         i = envc;
       }
       break;
diff --git a/tests/env.test b/tests/env.test
index 3098731d..4df118dd 100755
--- a/tests/env.test
+++ b/tests/env.test
@@ -19,3 +19,5 @@ unset WALRUS BANANA LETTERS FILTER

 testcmd "early fail" '--oops 2> /dev/null ; echo $?' "125\n" "" ""
 testcmd "why is this allowed" "=BLAH env | grep '^=BLAH\$'" "=BLAH\n" "" ""
+
+testcmd "replace" "A=foo PATH= `which printenv` A" "foo\n" "" ""
-- 
2.21.0.1020.gf2820cf01a-goog
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-env-fix-case-where-a-variable-is-replaced.patch
Type: text/x-patch
Size: 1436 bytes
Desc: not available
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20190502/e833e112/attachment-0002.bin>


More information about the Toybox mailing list