Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8ead39e

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

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
@@ -513,13 +513,14 @@ char *
513513
ecpg_gettext(constchar*msgid)
514514
{
515515
/*
516-
*If multiple threads come through here at about the same time, it's okay
517-
*for more than one of them tocall bindtextdomain().But it's not okay
518-
*for any of them to reach dgettext() before bindtextdomain() is
519-
*complete, so don't set the flag till that's done. Use "volatile" just
520-
*to be sure the compiler doesn't try to get cute.
516+
*At least on Windows, there are gettext implementations that fail if
517+
*multiple threadscall bindtextdomain() concurrently.Use a mutex and
518+
*flag variable to ensure that we call it just once per process. It is
519+
*not known that similar bugs exist on non-Windows platforms, but we
520+
*might as well do it the same way everywhere.
521521
*/
522522
staticvolatileboolalready_bound= false;
523+
staticpthread_mutex_tbinddomain_mutex=PTHREAD_MUTEX_INITIALIZER;
523524

524525
if (!already_bound)
525526
{
@@ -529,14 +530,26 @@ ecpg_gettext(const char *msgid)
529530
#else
530531
intsave_errno=errno;
531532
#endif
532-
constchar*ldir;
533-
534-
/* No relocatable lookup here because the binary could be anywhere */
535-
ldir=getenv("PGLOCALEDIR");
536-
if (!ldir)
537-
ldir=LOCALEDIR;
538-
bindtextdomain(PG_TEXTDOMAIN("ecpglib"),ldir);
539-
already_bound= true;
533+
534+
(void)pthread_mutex_lock(&binddomain_mutex);
535+
536+
if (!already_bound)
537+
{
538+
constchar*ldir;
539+
540+
/*
541+
* No relocatable lookup here because the calling executable could
542+
* be anywhere
543+
*/
544+
ldir=getenv("PGLOCALEDIR");
545+
if (!ldir)
546+
ldir=LOCALEDIR;
547+
bindtextdomain(PG_TEXTDOMAIN("ecpglib"),ldir);
548+
already_bound= true;
549+
}
550+
551+
(void)pthread_mutex_unlock(&binddomain_mutex);
552+
540553
#ifdefWIN32
541554
SetLastError(save_errno);
542555
#else

‎src/interfaces/libpq/fe-misc.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,13 +1240,14 @@ static void
12401240
libpq_binddomain(void)
12411241
{
12421242
/*
1243-
*If multiple threads come through here at about the same time, it's okay
1244-
*for more than one of them tocall 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.
1243+
*At least on Windows, there are gettext implementations that fail if
1244+
*multiple threadscall bindtextdomain() concurrently.Use a mutex and
1245+
*flag variable to ensure that we call it just once per process. It is
1246+
*not known that similar bugs exist on non-Windows platforms, but we
1247+
*might as well do it the same way everywhere.
12481248
*/
12491249
staticvolatileboolalready_bound= false;
1250+
staticpthread_mutex_tbinddomain_mutex=PTHREAD_MUTEX_INITIALIZER;
12501251

12511252
if (!already_bound)
12521253
{
@@ -1256,14 +1257,26 @@ libpq_binddomain(void)
12561257
#else
12571258
intsave_errno=errno;
12581259
#endif
1259-
constchar*ldir;
1260-
1261-
/* No relocatable lookup here because the binary could be anywhere */
1262-
ldir=getenv("PGLOCALEDIR");
1263-
if (!ldir)
1264-
ldir=LOCALEDIR;
1265-
bindtextdomain(PG_TEXTDOMAIN("libpq"),ldir);
1266-
already_bound= true;
1260+
1261+
(void)pthread_mutex_lock(&binddomain_mutex);
1262+
1263+
if (!already_bound)
1264+
{
1265+
constchar*ldir;
1266+
1267+
/*
1268+
* No relocatable lookup here because the calling executable could
1269+
* be anywhere
1270+
*/
1271+
ldir=getenv("PGLOCALEDIR");
1272+
if (!ldir)
1273+
ldir=LOCALEDIR;
1274+
bindtextdomain(PG_TEXTDOMAIN("libpq"),ldir);
1275+
already_bound= true;
1276+
}
1277+
1278+
(void)pthread_mutex_unlock(&binddomain_mutex);
1279+
12671280
#ifdefWIN32
12681281
SetLastError(save_errno);
12691282
#else

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp