Skip to content

Commit 37f5dc8

Browse files
committed
Fix race condition in gettext() initialization in libpq and ecpglib.
In libpq and ecpglib, multiple threads can concurrently enter the initialization logic for message localization. Since we set the its-done flag before actually doing the work, it'd be possible for some threads to reach gettext() before anyone has called bindtextdomain(). Barring bugs in libintl itself, this would not result in anything worse than failure to localize some early messages. Nonetheless, it's a bug, and an easy one to fix. Noted while investigating bug #17299 from Clemens Zeidler (much thanks to Liam Bowen for followup investigation on that). It currently appears that that actually *is* a bug in libintl itself, but that doesn't let us off the hook for this bit. Back-patch to all supported versions. Discussion: https://postgr.es/m/17299-7270741958c0b1ab@postgresql.org Discussion: https://postgr.es/m/CAE7q7Eit4Eq2=bxce=Fm8HAStECjaXUE=WBQc-sDDcgJQ7s7eg@mail.gmail.com
1 parent 2c15b29 commit 37f5dc8

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/interfaces/ecpg/ecpglib/misc.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,14 @@ win32_pthread_once(volatile pthread_once_t *once, void (*fn) (void))
489489
char *
490490
ecpg_gettext(const char *msgid)
491491
{
492-
static bool already_bound = false;
492+
/*
493+
* If multiple threads come through here at about the same time, it's okay
494+
* for more than one of them to call bindtextdomain(). But it's not okay
495+
* for any of them to reach dgettext() before bindtextdomain() is
496+
* complete, so don't set the flag till that's done. Use "volatile" just
497+
* to be sure the compiler doesn't try to get cute.
498+
*/
499+
static volatile bool already_bound = false;
493500

494501
if (!already_bound)
495502
{
@@ -501,12 +508,12 @@ ecpg_gettext(const char *msgid)
501508
#endif
502509
const char *ldir;
503510

504-
already_bound = true;
505511
/* No relocatable lookup here because the binary could be anywhere */
506512
ldir = getenv("PGLOCALEDIR");
507513
if (!ldir)
508514
ldir = LOCALEDIR;
509515
bindtextdomain(PG_TEXTDOMAIN("ecpglib"), ldir);
516+
already_bound = true;
510517
#ifdef WIN32
511518
SetLastError(save_errno);
512519
#else

src/interfaces/libpq/fe-misc.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,14 @@ PQenv2encoding(void)
12171217
static void
12181218
libpq_binddomain()
12191219
{
1220-
static bool already_bound = false;
1220+
/*
1221+
* If multiple threads come through here at about the same time, it's okay
1222+
* for more than one of them to call bindtextdomain(). But it's not okay
1223+
* for any of them to return to caller before bindtextdomain() is
1224+
* complete, so don't set the flag till that's done. Use "volatile" just
1225+
* to be sure the compiler doesn't try to get cute.
1226+
*/
1227+
static volatile bool already_bound = false;
12211228

12221229
if (!already_bound)
12231230
{
@@ -1229,12 +1236,12 @@ libpq_binddomain()
12291236
#endif
12301237
const char *ldir;
12311238

1232-
already_bound = true;
12331239
/* No relocatable lookup here because the binary could be anywhere */
12341240
ldir = getenv("PGLOCALEDIR");
12351241
if (!ldir)
12361242
ldir = LOCALEDIR;
12371243
bindtextdomain(PG_TEXTDOMAIN("libpq"), ldir);
1244+
already_bound = true;
12381245
#ifdef WIN32
12391246
SetLastError(save_errno);
12401247
#else

0 commit comments

Comments
 (0)