Skip to content

Commit 5762922

Browse files
committed
gettext: derive charset from the environment when gettext is uninitialized
is_utf8_locale() drives, among other things, whether `git grep -P -i` applies Unicode case-folding (grep.c sets PCRE2_UTF | PCRE2_UCP only when this returns true). It consults the static `charset` that git_setup_gettext() fills in via locale_charset(). That setup, however, bails out early when the locale directory cannot be found, leaving `charset` NULL. Because is_encoding_utf8(NULL) answers "yes", is_utf8_locale() then reports a UTF-8 locale unconditionally, even under LC_ALL=C, and `git grep -P -i` case-folds non-ASCII bytes when it must not. I ran into this while running the test suite with BusyBox' ash as the POSIX shell: in that leg the locale directory is not located, so t7816's "LC_ALL='C' git grep -P -i" cases that assert no Unicode folding (the matches_pcre2=0 rows) instead matched and their test_must_fail invocations failed. The MSYS2 Bash leg, where the locale directory is found and locale_charset() returns "C", was unaffected. The NO_GETTEXT build already protects against a NULL `charset` by deriving it from LC_ALL/LC_CTYPE/LANG. Apply that same fallback whenever `charset` is NULL rather than only in NO_GETTEXT builds, so a gettext-enabled build that could not initialize gettext still honors the locale from the environment. Assisted-by: Opus 4.8 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 15117b2 commit 5762922

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

gettext.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,13 @@ int gettext_width(const char *s)
141141

142142
int is_utf8_locale(void)
143143
{
144-
#ifdef NO_GETTEXT
144+
/*
145+
* The charset is normally set by git_setup_gettext(), but it bails out
146+
* early when the locale directory is missing, leaving `charset` NULL.
147+
* As is_encoding_utf8(NULL) answers "yes", that would make us assume a
148+
* UTF-8 locale even under e.g. LC_ALL=C, so derive the charset from the
149+
* environment ourselves in that case.
150+
*/
145151
if (!charset) {
146152
const char *env = getenv("LC_ALL");
147153
if (!env || !*env)
@@ -154,6 +160,5 @@ int is_utf8_locale(void)
154160
env = strchr(env, '.') + 1;
155161
charset = xstrdup(env);
156162
}
157-
#endif
158163
return is_encoding_utf8(charset);
159164
}

0 commit comments

Comments
 (0)