diff options
author | 2015-06-12 16:05:59 -0700 | |
---|---|---|
committer | 2015-06-12 16:11:45 -0700 | |
commit | ea407476d0e8bfe8b179dbb43bdecb6bdbd2acbb (patch) | |
tree | ab16df014471c46db19386752699fb08a3e98b30 | |
parent | b6b6de330463db1b79c9c7a67efd6d671b9b9de3 (diff) |
Correctly un-export an env var when it is shadowed
Prior to this fix, if you exported a variable in one scope
and then unexported it in the next, it would remain exported.
Example:
set -gx VAR 1
function foo; set -l VAR; env; end
foo
Here 'VAR' would be exported to 'env' because we failed to
notice that the env var is shadowed by an unexported variable.
This occurred at env var computation time, not in env_set!
Fixes #2132
-rw-r--r-- | env.cpp | 19 | ||||
-rw-r--r-- | tests/test3.in | 8 | ||||
-rw-r--r-- | tests/test3.out | 2 |
3 files changed, 24 insertions, 5 deletions
@@ -1264,7 +1264,7 @@ wcstring_list_t env_get_names(int flags) Get list of all exported variables */ -static void get_exported(const env_node_t *n, std::map<wcstring, wcstring> &h) +static void get_exported(const env_node_t *n, std::map<wcstring, wcstring> *h) { if (!n) return; @@ -1279,10 +1279,19 @@ static void get_exported(const env_node_t *n, std::map<wcstring, wcstring> &h) { const wcstring &key = iter->first; const var_entry_t &val_entry = iter->second; - if (val_entry.exportv && (val_entry.val != ENV_NULL)) + + if (val_entry.exportv && val_entry.val != ENV_NULL) + { + // Export the variable + // Don't use std::map::insert here, since we need to overwrite existing + // values from previous scopes + (*h)[key] = val_entry.val; + } + else { - // Don't use std::map::insert here, since we need to overwrite existing values from previous scopes - h[key] = val_entry.val; + // We need to erase from the map if we are not exporting, + // since a lower scope may have exported. See #2132 + h->erase(key); } } } @@ -1333,7 +1342,7 @@ static void update_export_array_if_necessary(bool recalc) debug(4, L"env_export_arr() recalc"); - get_exported(top, vals); + get_exported(top, &vals); if (uvars()) { diff --git a/tests/test3.in b/tests/test3.in index 970741d1..fe20928c 100644 --- a/tests/test3.in +++ b/tests/test3.in @@ -227,6 +227,14 @@ else echo Test 16 fail end +# Test that shadowing with a non-exported variable works +set -gx __fish_test_env17 UNSHADOWED +env | sgrep __fish_test_env17 +function __fish_test_shadow + set -l __fish_test_env17 + env | sgrep __fish_test_env17 ; or echo SHADOWED +end +__fish_test_shadow # clear for other shells set -eU __fish_test_universal_variables_variable_foo diff --git a/tests/test3.out b/tests/test3.out index a6d5d3ac..806ea708 100644 --- a/tests/test3.out +++ b/tests/test3.out @@ -16,6 +16,8 @@ Test 15 pass Foo change detected Foo change detected Test 16 pass +__fish_test_env17=UNSHADOWED +SHADOWED Testing Universal Startup 1 1 |