Skip to content

Commit 5521d81

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 1c6d055 commit 5521d81

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
@@ -496,7 +496,14 @@ win32_pthread_once(volatile pthread_once_t *once, void (*fn) (void))
496496
char *
497497
ecpg_gettext(const char *msgid)
498498
{
499-
static bool already_bound = false;
499+
/*
500+
* If multiple threads come through here at about the same time, it's okay
501+
* for more than one of them to call bindtextdomain(). But it's not okay
502+
* for any of them to reach dgettext() before bindtextdomain() is
503+
* complete, so don't set the flag till that's done. Use "volatile" just
504+
* to be sure the compiler doesn't try to get cute.
505+
*/
506+
static volatile bool already_bound = false;
500507

501508
if (!already_bound)
502509
{
@@ -508,12 +515,12 @@ ecpg_gettext(const char *msgid)
508515
#endif
509516
const char *ldir;
510517

511-
already_bound = true;
512518
/* No relocatable lookup here because the binary could be anywhere */
513519
ldir = getenv("PGLOCALEDIR");
514520
if (!ldir)
515521
ldir = LOCALEDIR;
516522
bindtextdomain(PG_TEXTDOMAIN("ecpglib"), ldir);
523+
already_bound = true;
517524
#ifdef WIN32
518525
SetLastError(save_errno);
519526
#else

src/interfaces/libpq/fe-misc.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,14 @@ PQenv2encoding(void)
12731273
static void
12741274
libpq_binddomain()
12751275
{
1276-
static bool already_bound = false;
1276+
/*
1277+
* If multiple threads come through here at about the same time, it's okay
1278+
* for more than one of them to call bindtextdomain(). But it's not okay
1279+
* for any of them to return to caller before bindtextdomain() is
1280+
* complete, so don't set the flag till that's done. Use "volatile" just
1281+
* to be sure the compiler doesn't try to get cute.
1282+
*/
1283+
static volatile bool already_bound = false;
12771284

12781285
if (!already_bound)
12791286
{
@@ -1285,12 +1292,12 @@ libpq_binddomain()
12851292
#endif
12861293
const char *ldir;
12871294

1288-
already_bound = true;
12891295
/* No relocatable lookup here because the binary could be anywhere */
12901296
ldir = getenv("PGLOCALEDIR");
12911297
if (!ldir)
12921298
ldir = LOCALEDIR;
12931299
bindtextdomain(PG_TEXTDOMAIN("libpq"), ldir);
1300+
already_bound = true;
12941301
#ifdef WIN32
12951302
SetLastError(save_errno);
12961303
#else

0 commit comments

Comments
 (0)