Skip to content

Commit c4137be

Browse files
committed
gettext: avoid using gettext if the locale dir is not present
In cc5e1bf (gettext: avoid initialization if the locale dir is not present, 2018-04-21) Git was taught to avoid a costly gettext start-up when there are not even any localized messages to work with. But we still called `gettext()` and `ngettext()` functions. Which caused a problem in Git for Windows when the libgettext that is consumed from the MSYS2 project stopped using a runtime prefix in msys2/MINGW-packages#10461 Due to that change, we now use an unintialized gettext machinery that might get auto-initialized _using an unintended locale directory_: `C:\mingw64\share\locale`. Let's record the fact when the gettext initialization was skipped, and skip calling the gettext functions accordingly. This addresses CVE-2023-25815. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 2f3b28f commit c4137be

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

gettext.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ static void init_gettext_charset(const char *domain)
109109
setlocale(LC_CTYPE, "C");
110110
}
111111

112+
int git_gettext_enabled = 0;
113+
112114
void git_setup_gettext(void)
113115
{
114116
const char *podir = getenv(GIT_TEXT_DOMAIN_DIR_ENVIRONMENT);
@@ -130,6 +132,8 @@ void git_setup_gettext(void)
130132
init_gettext_charset("git");
131133
textdomain("git");
132134

135+
git_gettext_enabled = 1;
136+
133137
free(p);
134138
}
135139

gettext.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
int use_gettext_poison(void);
3232

3333
#ifndef NO_GETTEXT
34+
extern int git_gettext_enabled;
3435
void git_setup_gettext(void);
3536
int gettext_width(const char *s);
3637
#else
38+
#define git_gettext_enabled (0)
3739
static inline void git_setup_gettext(void)
3840
{
3941
use_gettext_poison(); /* getenv() reentrancy paranoia */
@@ -48,14 +50,17 @@ static inline FORMAT_PRESERVING(1) const char *_(const char *msgid)
4850
{
4951
if (!*msgid)
5052
return "";
51-
return use_gettext_poison() ? "# GETTEXT POISON #" : gettext(msgid);
53+
return use_gettext_poison() ? "# GETTEXT POISON #" :
54+
!git_gettext_enabled ? msgid : gettext(msgid);
5255
}
5356

5457
static inline FORMAT_PRESERVING(1) FORMAT_PRESERVING(2)
5558
const char *Q_(const char *msgid, const char *plu, unsigned long n)
5659
{
5760
if (use_gettext_poison())
5861
return "# GETTEXT POISON #";
62+
if (!git_gettext_enabled)
63+
return n == 1 ? msgid : plu;
5964
return ngettext(msgid, plu, n);
6065
}
6166

0 commit comments

Comments
 (0)