diff options
author | ridiculousfish <corydoras@ridiculousfish.com> | 2015-06-12 16:05:59 -0700 |
---|---|---|
committer | ridiculousfish <corydoras@ridiculousfish.com> | 2015-06-12 16:05:59 -0700 |
commit | f3560b8e626a528f2b5cc167c00d610ab9546b82 (patch) | |
tree | 665ba82b461fcd545b8d8e064eb077e5e9cf6511 /env.cpp | |
parent | 18d74655925eb7afb133b1b1437b901ae816e33e (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
Diffstat (limited to 'env.cpp')
-rw-r--r-- | env.cpp | 19 |
1 files changed, 14 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()) { |