diff options
author | green <green@FreeBSD.org> | 2009-12-01 05:04:31 +0000 |
---|---|---|
committer | green <green@FreeBSD.org> | 2009-12-01 05:04:31 +0000 |
commit | 32cc2b56a77bafdbaa36b298213654cc42281683 (patch) | |
tree | 4dd69d3b32100a3a44a49bb2deca1681144fe2ab /tools | |
parent | dbccfb34d9a4442db60f517065d3c3f6596eb745 (diff) | |
download | FreeBSD-src-32cc2b56a77bafdbaa36b298213654cc42281683.zip FreeBSD-src-32cc2b56a77bafdbaa36b298213654cc42281683.tar.gz |
Do not gratuitously fail *env(3) operations due to corrupt ('='-less)
**environ entries. This puts non-getenv(3) operations in line with
getenv(3) in that bad environ entries do not cause all operations to
fail. There is still some inconsistency in that getenv(3) in the
absence of any environment-modifying operation does not emit corrupt
environ entry warnings.
I also fixed another inconsistency in getenv(3) where updating the
global environ pointer would not be reflected in the return values.
It would have taken an intermediary setenv(3)/putenv(3)/unsetenv(3)
in order to see the change.
Diffstat (limited to 'tools')
-rw-r--r-- | tools/regression/environ/Makefile.envctl | 2 | ||||
-rw-r--r-- | tools/regression/environ/envctl.c | 31 | ||||
-rw-r--r-- | tools/regression/environ/envtest.t | 15 |
3 files changed, 45 insertions, 3 deletions
diff --git a/tools/regression/environ/Makefile.envctl b/tools/regression/environ/Makefile.envctl index 6298a20..fc90499 100644 --- a/tools/regression/environ/Makefile.envctl +++ b/tools/regression/environ/Makefile.envctl @@ -13,4 +13,4 @@ NO_MAN= yes .include <bsd.prog.mk> test: ${PROG} - @sh envtest.t + @env -i sh envtest.t diff --git a/tools/regression/environ/envctl.c b/tools/regression/environ/envctl.c index 7858211..586314b 100644 --- a/tools/regression/environ/envctl.c +++ b/tools/regression/environ/envctl.c @@ -60,7 +60,7 @@ dump_environ(void) static void usage(const char *program) { - fprintf(stderr, "Usage: %s [-DGUchrt] [-c 1|2|3|4] [-gu name] " + fprintf(stderr, "Usage: %s [-DGUchrt] [-c 1|2|3|4] [-bgu name] " "[-p name=value]\n" "\t[(-S|-s name) value overwrite]\n\n" "Options:\n" @@ -68,6 +68,7 @@ usage(const char *program) " -G name\t\t\tgetenv(NULL)\n" " -S value overwrite\t\tsetenv(NULL, value, overwrite)\n" " -U\t\t\t\tunsetenv(NULL)\n" + " -b name\t\t\tblank the 'name=$name' entry, corrupting it\n" " -c 1|2|3|4\t\t\tClear environ variable using method:\n" "\t\t\t\t1 - set environ to NULL pointer\n" "\t\t\t\t2 - set environ[0] to NULL pointer\n" @@ -98,6 +99,28 @@ print_rtrn_errno(int rtrnVal, const char *eol) return; } +static void +blank_env(const char *var) +{ + char **newenviron; + int n, varlen; + + if (environ == NULL) + return; + + for (n = 0; environ[n] != NULL; n++) + ; + newenviron = malloc(sizeof(char *) * (n + 1)); + varlen = strlen(var); + for (; n >= 0; n--) { + newenviron[n] = environ[n]; + if (newenviron[n] != NULL && + strncmp(newenviron[n], var, varlen) == 0 && + newenviron[n][varlen] == '=') + newenviron[n] += strlen(newenviron[n]); + } + environ = newenviron; +} int main(int argc, char **argv) @@ -114,8 +137,12 @@ main(int argc, char **argv) } /* The entire program is basically executed from this loop. */ - while ((arg = getopt(argc, argv, "DGS:Uc:g:hp:rs:tu:")) != -1) { + while ((arg = getopt(argc, argv, "DGS:Ub:c:g:hp:rs:tu:")) != -1) { switch (arg) { + case 'b': + blank_env(optarg); + break; + case 'c': switch (atoi(optarg)) { case 1: diff --git a/tools/regression/environ/envtest.t b/tools/regression/environ/envtest.t index 6b5485a..2c54017 100644 --- a/tools/regression/environ/envtest.t +++ b/tools/regression/environ/envtest.t @@ -232,3 +232,18 @@ check_result "${BAR} 0 0 ${BAR} 0 0 ${NULL}" run_test -r -g FOO -u FOO -g FOO -s FOO ${BAR} 1 -g FOO check_result "${BAR} 0 0 ${NULL} 0 0 ${BAR}" + + +# corruption (blanking) of environ members. +export BLANK_ME= +export AFTER_BLANK=blanked +run_test -b BLANK_ME -p MORE=vars -g FOO -g BLANK_ME -g AFTER_BLANK +check_result "0 0 ${FOO} ${NULL} ${AFTER_BLANK}" + +run_test -b BLANK_ME -u FOO -g FOO -g AFTER_BLANK +check_result "0 0 ${NULL} ${AFTER_BLANK}" + +export BLANK_ME2= +export AFTER_BLANKS=blankD +run_test -b BLANK_ME -b AFTER_BLANK -b BLANK_ME2 -g FOO -g AFTER_BLANKS +check_result "${FOO} ${AFTER_BLANKS}" |