fix use after free bug

Fixes #3466
This commit is contained in:
Kurtis Rader 2016-10-16 12:43:48 -07:00
parent d5ca88d42d
commit 4f397e86d7

View file

@ -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.