env: fix case where a variable is replaced.

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

Also add the corresponding test.
This commit is contained in:
Elliott Hughes 2019-05-02 18:41:11 -07:00 committed by Rob Landley
parent d234c98318
commit bd7c5c0555
2 changed files with 3 additions and 3 deletions

View File

@ -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;

View File

@ -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" "" ""