Skip to content

Commit 9fb1396

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 95e960e commit 9fb1396

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
@@ -518,13 +518,14 @@ char *
518518
ecpg_gettext(const char *msgid)
519519
{
520520
/*
521-
* If multiple threads come through here at about the same time, it's okay
522-
* for more than one of them to call bindtextdomain(). But it's not okay
523-
* for any of them to reach dgettext() before bindtextdomain() is
524-
* complete, so don't set the flag till that's done. Use "volatile" just
525-
* to be sure the compiler doesn't try to get cute.
521+
* At least on Windows, there are gettext implementations that fail if
522+
* multiple threads call bindtextdomain() concurrently. Use a mutex and
523+
* flag variable to ensure that we call it just once per process. It is
524+
* not known that similar bugs exist on non-Windows platforms, but we
525+
* might as well do it the same way everywhere.
526526
*/
527527
static volatile bool already_bound = false;
528+
static pthread_mutex_t binddomain_mutex = PTHREAD_MUTEX_INITIALIZER;
528529

529530
if (!already_bound)
530531
{
@@ -534,14 +535,26 @@ ecpg_gettext(const char *msgid)
534535
#else
535536
int save_errno = errno;
536537
#endif
537-
const char *ldir;
538-
539-
/* No relocatable lookup here because the binary could be anywhere */
540-
ldir = getenv("PGLOCALEDIR");
541-
if (!ldir)
542-
ldir = LOCALEDIR;
543-
bindtextdomain(PG_TEXTDOMAIN("ecpglib"), ldir);
544-
already_bound = true;
538+
539+
(void) pthread_mutex_lock(&binddomain_mutex);
540+
541+
if (!already_bound)
542+
{
543+
const char *ldir;
544+
545+
/*
546+
* No relocatable lookup here because the calling executable could
547+
* be anywhere
548+
*/
549+
ldir = getenv("PGLOCALEDIR");
550+
if (!ldir)
551+
ldir = LOCALEDIR;
552+
bindtextdomain(PG_TEXTDOMAIN("ecpglib"), ldir);
553+
already_bound = true;
554+
}
555+
556+
(void) pthread_mutex_unlock(&binddomain_mutex);
557+
545558
#ifdef WIN32
546559
SetLastError(save_errno);
547560
#else

src/interfaces/libpq/fe-misc.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,13 +1274,14 @@ static void
12741274
libpq_binddomain()
12751275
{
12761276
/*
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.
1277+
* At least on Windows, there are gettext implementations that fail if
1278+
* multiple threads call bindtextdomain() concurrently. Use a mutex and
1279+
* flag variable to ensure that we call it just once per process. It is
1280+
* not known that similar bugs exist on non-Windows platforms, but we
1281+
* might as well do it the same way everywhere.
12821282
*/
12831283
static volatile bool already_bound = false;
1284+
static pthread_mutex_t binddomain_mutex = PTHREAD_MUTEX_INITIALIZER;
12841285

12851286
if (!already_bound)
12861287
{
@@ -1290,14 +1291,26 @@ libpq_binddomain()
12901291
#else
12911292
int save_errno = errno;
12921293
#endif
1293-
const char *ldir;
1294-
1295-
/* No relocatable lookup here because the binary could be anywhere */
1296-
ldir = getenv("PGLOCALEDIR");
1297-
if (!ldir)
1298-
ldir = LOCALEDIR;
1299-
bindtextdomain(PG_TEXTDOMAIN("libpq"), ldir);
1300-
already_bound = true;
1294+
1295+
(void) pthread_mutex_lock(&binddomain_mutex);
1296+
1297+
if (!already_bound)
1298+
{
1299+
const char *ldir;
1300+
1301+
/*
1302+
* No relocatable lookup here because the calling executable could
1303+
* be anywhere
1304+
*/
1305+
ldir = getenv("PGLOCALEDIR");
1306+
if (!ldir)
1307+
ldir = LOCALEDIR;
1308+
bindtextdomain(PG_TEXTDOMAIN("libpq"), ldir);
1309+
already_bound = true;
1310+
}
1311+
1312+
(void) pthread_mutex_unlock(&binddomain_mutex);
1313+
13011314
#ifdef WIN32
13021315
SetLastError(save_errno);
13031316
#else

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy