Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a584d03

Browse files
committed
Avoid concurrent calls to bindtextdomain().
We previously supposed that it was okay for different threads to call bindtextdomain() concurrently (cf. commit 1f655fd). It now emerges that there's at least one gettext implementation in which that triggers an abort() crash, so let's stop doing that. Add mutexes guarding libpq's and ecpglib's calls, which are the only ones that need worry about multithreaded callers. Note: in libpq, we could perhaps have piggybacked on default_threadlock() to avoid defining a new mutex variable. I judge that not terribly safe though, since libpq_gettext could be called from code that is holding the default mutex. If that were the first such call in the process, it'd fail. An extra mutex is cheap insurance against unforeseen interactions. Per bug #18312 from Christian Maurer. Back-patch to all supported versions. Discussion: https://postgr.es/m/18312-bbbabc8113592b78@postgresql.org Discussion: https://postgr.es/m/264860.1707163416@sss.pgh.pa.us
1 parent 0028b55 commit a584d03

File tree

2 files changed

+52
-26
lines changed

2 files changed

+52
-26
lines changed

src/interfaces/ecpg/ecpglib/misc.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,14 @@ char *
465465
ecpg_gettext(const char *msgid)
466466
{
467467
/*
468-
* If multiple threads come through here at about the same time, it's okay
469-
* for more than one of them to call bindtextdomain(). But it's not okay
470-
* for any of them to reach dgettext() before bindtextdomain() is
471-
* complete, so don't set the flag till that's done. Use "volatile" just
472-
* to be sure the compiler doesn't try to get cute.
468+
* At least on Windows, there are gettext implementations that fail if
469+
* multiple threads call bindtextdomain() concurrently. Use a mutex and
470+
* flag variable to ensure that we call it just once per process. It is
471+
* not known that similar bugs exist on non-Windows platforms, but we
472+
* might as well do it the same way everywhere.
473473
*/
474474
static volatile bool already_bound = false;
475+
static pthread_mutex_t binddomain_mutex = PTHREAD_MUTEX_INITIALIZER;
475476

476477
if (!already_bound)
477478
{
@@ -481,14 +482,26 @@ ecpg_gettext(const char *msgid)
481482
#else
482483
int save_errno = errno;
483484
#endif
484-
const char *ldir;
485-
486-
/* No relocatable lookup here because the binary could be anywhere */
487-
ldir = getenv("PGLOCALEDIR");
488-
if (!ldir)
489-
ldir = LOCALEDIR;
490-
bindtextdomain(PG_TEXTDOMAIN("ecpglib"), ldir);
491-
already_bound = true;
485+
486+
(void) pthread_mutex_lock(&binddomain_mutex);
487+
488+
if (!already_bound)
489+
{
490+
const char *ldir;
491+
492+
/*
493+
* No relocatable lookup here because the calling executable could
494+
* be anywhere
495+
*/
496+
ldir = getenv("PGLOCALEDIR");
497+
if (!ldir)
498+
ldir = LOCALEDIR;
499+
bindtextdomain(PG_TEXTDOMAIN("ecpglib"), ldir);
500+
already_bound = true;
501+
}
502+
503+
(void) pthread_mutex_unlock(&binddomain_mutex);
504+
492505
#ifdef WIN32
493506
SetLastError(save_errno);
494507
#else

src/interfaces/libpq/fe-misc.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,13 +1225,14 @@ static void
12251225
libpq_binddomain(void)
12261226
{
12271227
/*
1228-
* If multiple threads come through here at about the same time, it's okay
1229-
* for more than one of them to call bindtextdomain(). But it's not okay
1230-
* for any of them to return to caller before bindtextdomain() is
1231-
* complete, so don't set the flag till that's done. Use "volatile" just
1232-
* to be sure the compiler doesn't try to get cute.
1228+
* At least on Windows, there are gettext implementations that fail if
1229+
* multiple threads call bindtextdomain() concurrently. Use a mutex and
1230+
* flag variable to ensure that we call it just once per process. It is
1231+
* not known that similar bugs exist on non-Windows platforms, but we
1232+
* might as well do it the same way everywhere.
12331233
*/
12341234
static volatile bool already_bound = false;
1235+
static pthread_mutex_t binddomain_mutex = PTHREAD_MUTEX_INITIALIZER;
12351236

12361237
if (!already_bound)
12371238
{
@@ -1241,14 +1242,26 @@ libpq_binddomain(void)
12411242
#else
12421243
int save_errno = errno;
12431244
#endif
1244-
const char *ldir;
1245-
1246-
/* No relocatable lookup here because the binary could be anywhere */
1247-
ldir = getenv("PGLOCALEDIR");
1248-
if (!ldir)
1249-
ldir = LOCALEDIR;
1250-
bindtextdomain(PG_TEXTDOMAIN("libpq"), ldir);
1251-
already_bound = true;
1245+
1246+
(void) pthread_mutex_lock(&binddomain_mutex);
1247+
1248+
if (!already_bound)
1249+
{
1250+
const char *ldir;
1251+
1252+
/*
1253+
* No relocatable lookup here because the calling executable could
1254+
* be anywhere
1255+
*/
1256+
ldir = getenv("PGLOCALEDIR");
1257+
if (!ldir)
1258+
ldir = LOCALEDIR;
1259+
bindtextdomain(PG_TEXTDOMAIN("libpq"), ldir);
1260+
already_bound = true;
1261+
}
1262+
1263+
(void) pthread_mutex_unlock(&binddomain_mutex);
1264+
12521265
#ifdef WIN32
12531266
SetLastError(save_errno);
12541267
#else

0 commit comments

Comments
 (0)