diff --git a/src/env.cpp b/src/env.cpp index f34ee9c27..79a1e0f0c 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -191,7 +191,9 @@ static bool var_is_locale(const wcstring &key) { /// Properly sets all locale information. static void handle_locale(const wchar_t *env_var_name) { debug(2, L"handle_locale() called in response to '%ls' changing", env_var_name); - const char *old_msg_locale = setlocale(LC_MESSAGES, NULL); + // We have to make a copy because the subsequent setlocale() call to change the locale will + // invalidate the pointer from the this setlocale() call. + char *old_msg_locale = strdup(setlocale(LC_MESSAGES, NULL)); const env_var_t val = env_get_string(env_var_name, ENV_EXPORT); const std::string &value = wcs2string(val); const std::string &name = wcs2string(env_var_name); @@ -216,6 +218,7 @@ static void handle_locale(const wchar_t *env_var_name) { _nl_msg_cat_cntr++; } #endif + free(old_msg_locale); } /// Check if the specified variable is a locale variable.