From afd8d2f9bae14cb3c263987a01aedb748b975176 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 16 Jun 2012 13:05:58 -0700 Subject: [PATCH] Fix for https://github.com/fish-shell/fish-shell/issues/135 Don't use std::map::insert when we need to overwrite values --- env.cpp | 13 ++++++------- env_universal_common.cpp | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/env.cpp b/env.cpp index dd44b33c5..6e6a8d741 100644 --- a/env.cpp +++ b/env.cpp @@ -901,8 +901,7 @@ int env_set(const wcstring &key, const wchar_t *val, int var_mode) } entry->val = val; - - node->env.insert(std::pair(key, entry)); + node->env[key] = entry; if( entry->exportv ) { @@ -1397,7 +1396,8 @@ static void get_exported( const env_node_t *n, std::map &h ) var_entry_t *val_entry = iter->second; if( val_entry->exportv && (val_entry->val != ENV_NULL ) ) { - h.insert(std::pair(key, val_entry->val)); + // Don't use std::map::insert here, since we need to overwrite existing values from previous scopes + h[key] = val_entry->val; } } } @@ -1455,12 +1455,11 @@ static void update_export_array_if_necessary(bool recalc) { const wcstring &key = uni.at(i); const wchar_t *val = env_universal_get( key.c_str() ); - std::map::iterator result = vals.find( key ); - if( wcscmp( val, ENV_NULL) && ( result == vals.end() ) ) - { + if (wcscmp( val, ENV_NULL)) { + // Note that std::map::insert does NOT overwrite a value already in the map, + // which we depend on here vals.insert(std::pair(key, val)); } - } std::vector local_export_buffer; diff --git a/env_universal_common.cpp b/env_universal_common.cpp index 8704cc70d..d80a4d0d1 100644 --- a/env_universal_common.cpp +++ b/env_universal_common.cpp @@ -555,7 +555,7 @@ void env_universal_common_set( const wchar_t *key, const wchar_t *val, int expor entry->val = val; env_universal_common_remove( key ); - env_universal_var.insert( std::pair(key, entry)); + env_universal_var[key] = entry; if( callback ) { callback( exportv?SET_EXPORT:SET, key, val );