Skip to content

Commit 64ebb43

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 fd48e5f commit 64ebb43

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

496503
if (!already_bound)
497504
{
@@ -503,12 +510,12 @@ ecpg_gettext(const char *msgid)
503510
#endif
504511
const char *ldir;
505512

506-
already_bound = true;
507513
/* No relocatable lookup here because the binary could be anywhere */
508514
ldir = getenv("PGLOCALEDIR");
509515
if (!ldir)
510516
ldir = LOCALEDIR;
511517
bindtextdomain(PG_TEXTDOMAIN("ecpglib"), ldir);
518+
already_bound = true;
512519
#ifdef WIN32
513520
SetLastError(save_errno);
514521
#else

src/interfaces/libpq/fe-misc.c

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

12761283
if (!already_bound)
12771284
{
@@ -1283,12 +1290,12 @@ libpq_binddomain(void)
12831290
#endif
12841291
const char *ldir;
12851292

1286-
already_bound = true;
12871293
/* No relocatable lookup here because the binary could be anywhere */
12881294
ldir = getenv("PGLOCALEDIR");
12891295
if (!ldir)
12901296
ldir = LOCALEDIR;
12911297
bindtextdomain(PG_TEXTDOMAIN("libpq"), ldir);
1298+
already_bound = true;
12921299
#ifdef WIN32
12931300
SetLastError(save_errno);
12941301
#else

0 commit comments

Comments
 (0)