From 3cd80f118de32260fdaf25a6d7afd5729bbc0fb1 Mon Sep 17 00:00:00 2001 From: gambas Date: Tue, 26 Feb 2019 20:59:03 +0100 Subject: [PATCH] Allocate custom language environment variables statically, because environment is accessed by shared library exit routines, after the interpreter memory allocations have been freed. [INTERPRETER] * BUG: Allocate custom language environment variables statically, because environment is accessed by shared library exit routines, after the interpreter memory allocations have been freed. --- main/gbx/gbx_local.c | 56 +++++++++++++------------------------------- 1 file changed, 16 insertions(+), 40 deletions(-) diff --git a/main/gbx/gbx_local.c b/main/gbx/gbx_local.c index 38bb9f540..d6712b338 100644 --- a/main/gbx/gbx_local.c +++ b/main/gbx/gbx_local.c @@ -111,9 +111,9 @@ static bool _translation_loaded = FALSE; static LOCAL_INFO *local_current; -static char *env_LC_ALL = NULL; -static char *env_LANGUAGE = NULL; -static char *env_LANG = NULL; +static char env_LC_ALL[MAX_LANG + sizeof "LC_ALL" + 1]; +static char env_LANGUAGE[MAX_LANG + sizeof "LANGUAGE" + 1]; +static char env_LANG[MAX_LANG + sizeof "LANG" + 1]; static bool _currency; @@ -153,25 +153,10 @@ static bool is_currency_space(bool negative, bool intl) return test_currency_flag(negative, TRUE, FALSE, intl); } -static int my_setenv(const char *name, const char *value, char **ptr) +static void my_setenv(const char *name, const char *value, char *ptr) { - char *str = NULL; - - str = STRING_add(str, name, 0); - str = STRING_add_char(str, '='); - str = STRING_add(str, value, 0); - - if (putenv(str)) - { - STRING_free(&str); - return 1; - } - else - { - STRING_free(ptr); - *ptr = str; - return 0; - } + snprintf(ptr, MAX_LANG + strlen(name) + 1, "%s=%s", name, value); + putenv(ptr); } static void begin(void) @@ -606,23 +591,11 @@ void LOCAL_init(void) void LOCAL_exit(void) { - if (env_LANG) + if (environ == _environ) { - if (environ == _environ) - unsetenv("LANG"); - STRING_free(&env_LANG); - } - if (env_LC_ALL) - { - if (environ == _environ) - unsetenv("LC_ALL"); - STRING_free(&env_LC_ALL); - } - if (env_LANGUAGE) - { - if (environ == _environ) - unsetenv("LANGUAGE"); - STRING_free(&env_LANGUAGE); + unsetenv("LANG"); + unsetenv("LC_ALL"); + unsetenv("LANGUAGE"); } if (!LOCAL_is_UTF8) @@ -657,21 +630,24 @@ void LOCAL_set_lang(const char *lang) char *var; char *err; + if (lang && (strlen(lang) > MAX_LANG)) + THROW(E_ARG); + #ifdef DEBUG_LANG fprintf(stderr, "******** LOCAL_set_lang: %s ********\n", lang); #endif if (lang && *lang) { - my_setenv("LANG", lang, &env_LANG); - my_setenv("LC_ALL", lang, &env_LC_ALL); + my_setenv("LANG", lang, env_LANG); + my_setenv("LC_ALL", lang, env_LC_ALL); } STRING_free(&_lang); lang = LOCAL_get_lang(); if (getenv("LANGUAGE")) - my_setenv("LANGUAGE", lang, &env_LANGUAGE); + my_setenv("LANGUAGE", lang, env_LANGUAGE); if (setlocale(LC_ALL, "")) {