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

Commit9fb1396

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 parent95e960e commit9fb1396

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(constchar*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 tocall 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 threadscall 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
staticvolatileboolalready_bound= false;
528+
staticpthread_mutex_tbinddomain_mutex=PTHREAD_MUTEX_INITIALIZER;
528529

529530
if (!already_bound)
530531
{
@@ -534,14 +535,26 @@ ecpg_gettext(const char *msgid)
534535
#else
535536
intsave_errno=errno;
536537
#endif
537-
constchar*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+
constchar*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
#ifdefWIN32
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 tocall 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 threadscall 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
staticvolatileboolalready_bound= false;
1284+
staticpthread_mutex_tbinddomain_mutex=PTHREAD_MUTEX_INITIALIZER;
12841285

12851286
if (!already_bound)
12861287
{
@@ -1290,14 +1291,26 @@ libpq_binddomain()
12901291
#else
12911292
intsave_errno=errno;
12921293
#endif
1293-
constchar*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+
constchar*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
#ifdefWIN32
13021315
SetLastError(save_errno);
13031316
#else

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp