Skip to content

Commit 2131c04

Browse files
committed
Avoid calling gettext() in signal handlers.
It seems highly unlikely that gettext() can be relied on to be async-signal-safe. psql used to understand that, but someone got it wrong long ago in the src/bin/scripts/ version of handle_sigint, and then the bad idea was perpetuated when those two versions were unified into src/fe_utils/cancel.c. I'm unsure why there have not been field complaints about this ... maybe gettext() is signal-safe once it's translated at least one message? But we have no business assuming any such thing. In cancel.c (v13 and up), I preserved our ability to localize "Cancel request sent" messages by invoking gettext() before the signal handler is set up. In earlier branches I just made src/bin/scripts/ not localize those messages, as psql did then. (Just for extra unsafety, the src/bin/scripts/ version was invoking fprintf() from a signal handler. Sigh.) Noted while fixing signal-safety issues in PQcancel() itself. Back-patch to all supported branches. Discussion: https://postgr.es/m/2937814.1641960929@sss.pgh.pa.us
1 parent f3f467b commit 2131c04

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/fe_utils/cancel.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
*/
4343
static PGcancel *volatile cancelConn = NULL;
4444

45+
/*
46+
* Predetermined localized error strings --- needed to avoid trying
47+
* to call gettext() from a signal handler.
48+
*/
49+
static const char *cancel_sent_msg = NULL;
50+
static const char *cancel_not_sent_msg = NULL;
51+
4552
/*
4653
* CancelRequested is set when we receive SIGINT (or local equivalent).
4754
* There is no provision in this module for resetting it; but applications
@@ -158,11 +165,11 @@ handle_sigint(SIGNAL_ARGS)
158165
{
159166
if (PQcancel(cancelConn, errbuf, sizeof(errbuf)))
160167
{
161-
write_stderr(_("Cancel request sent\n"));
168+
write_stderr(cancel_sent_msg);
162169
}
163170
else
164171
{
165-
write_stderr(_("Could not send cancel request: "));
172+
write_stderr(cancel_not_sent_msg);
166173
write_stderr(errbuf);
167174
}
168175
}
@@ -179,6 +186,9 @@ void
179186
setup_cancel_handler(void (*callback) (void))
180187
{
181188
cancel_callback = callback;
189+
cancel_sent_msg = _("Cancel request sent\n");
190+
cancel_not_sent_msg = _("Could not send cancel request: ");
191+
182192
pqsignal(SIGINT, handle_sigint);
183193
}
184194

@@ -203,11 +213,11 @@ consoleHandler(DWORD dwCtrlType)
203213
{
204214
if (PQcancel(cancelConn, errbuf, sizeof(errbuf)))
205215
{
206-
write_stderr(_("Cancel request sent\n"));
216+
write_stderr(cancel_sent_msg);
207217
}
208218
else
209219
{
210-
write_stderr(_("Could not send cancel request: "));
220+
write_stderr(cancel_not_sent_msg);
211221
write_stderr(errbuf);
212222
}
213223
}
@@ -225,6 +235,8 @@ void
225235
setup_cancel_handler(void (*callback) (void))
226236
{
227237
cancel_callback = callback;
238+
cancel_sent_msg = _("Cancel request sent\n");
239+
cancel_not_sent_msg = _("Could not send cancel request: ");
228240

229241
InitializeCriticalSection(&cancelConnLock);
230242

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