Skip to content

Commit 1f655fd

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 1fabec7 commit 1f655fd

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
@@ -1239,7 +1239,14 @@ PQenv2encoding(void)
12391239
static void
12401240
libpq_binddomain(void)
12411241
{
1242-
static bool already_bound = false;
1242+
/*
1243+
* If multiple threads come through here at about the same time, it's okay
1244+
* for more than one of them to call bindtextdomain(). But it's not okay
1245+
* for any of them to return to caller before bindtextdomain() is
1246+
* complete, so don't set the flag till that's done. Use "volatile" just
1247+
* to be sure the compiler doesn't try to get cute.
1248+
*/
1249+
static volatile bool already_bound = false;
12431250

12441251
if (!already_bound)
12451252
{
@@ -1251,12 +1258,12 @@ libpq_binddomain(void)
12511258
#endif
12521259
const char *ldir;
12531260

1254-
already_bound = true;
12551261
/* No relocatable lookup here because the binary could be anywhere */
12561262
ldir = getenv("PGLOCALEDIR");
12571263
if (!ldir)
12581264
ldir = LOCALEDIR;
12591265
bindtextdomain(PG_TEXTDOMAIN("libpq"), ldir);
1266+
already_bound = true;
12601267
#ifdef WIN32
12611268
SetLastError(save_errno);
12621269
#else

0 commit comments

Comments
 (0)