[GSoC Patch 1/2] gettext: fall back to env-derived charset when unset
From: K Jayatheerth <hidden>
Date: 2026-08-21 13:55:10
Subsystem:
the rest · Maintainer:
Linus Torvalds
`is_utf8_locale()` relies on the static `charset` variable, which is normally initialized by `init_gettext_charset()`. That initialization only happens when `git_setup_gettext()` successfully locates the locale directory. When running directly from the source tree without `make install`, or in other environments where the locale directory is unavailable, `git_setup_gettext()` returns early, leaving `charset` unset (NULL). Because `is_encoding_utf8(NULL)` defaults to 1, `is_utf8_locale()` would mistakenly report a UTF-8 locale even in non-UTF-8 environments (e.g. under `LC_ALL=C`). The fallback that derives the charset from `LC_ALL`, `LC_CTYPE`, or `LANG` was previously compiled only under `NO_GETTEXT`. That left gettext-enabled builds without a fallback when `charset` remains uninitialized. Make the fallback conditional on `charset` being unset rather than on `NO_GETTEXT`. This ensures `is_utf8_locale()` accurately inspects the environment-derived charset regardless of whether gettext support is enabled. Mentored-by: Justin Tobler [off-list ref] Mentored-by: Lucas Seiki Oshiro [off-list ref] Signed-off-by: K Jayatheerth <redacted> --- gettext.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-)
diff --git a/gettext.c b/gettext.c
index 8d08a61f84..5376a0de0f 100644
--- a/gettext.c
+++ b/gettext.c@@ -141,19 +141,23 @@ int gettext_width(const char *s) int is_utf8_locale(void) { -#ifdef NO_GETTEXT - if (!charset) { - const char *env = getenv("LC_ALL"); - if (!env || !*env) - env = getenv("LC_CTYPE"); - if (!env || !*env) - env = getenv("LANG"); - if (!env) - env = ""; - if (strchr(env, '.')) - env = strchr(env, '.') + 1; - charset = xstrdup(env); + const char *c = charset; + + if (!c) { + static char fallback_charset[64]; + if (!*fallback_charset) { + const char *env = getenv("LC_ALL"); + if (!env || !*env) + env = getenv("LC_CTYPE"); + if (!env || !*env) + env = getenv("LANG"); + if (!env) + env = ""; + if (strchr(env, '.')) + env = strchr(env, '.') + 1; + strlcpy(fallback_charset, env, sizeof(fallback_charset)); + } + c = fallback_charset; } -#endif - return is_encoding_utf8(charset); + return is_encoding_utf8(c); }
--
2.55.GIT