From dcbec53f4d90af369192695ee53f3b97e492b3bf Mon Sep 17 00:00:00 2001 From: Noah Misch <noah@leadboat.com> Date: Sun, 12 Nov 2017 13:03:15 -0800 Subject: [PATCH] Don't call pgwin32_message_to_UTF16() without CurrentMemoryContext. PostgreSQL running as a Windows service crashed upon calling write_stderr() before MemoryContextInit(). This fix completes work started in 5735efee15540765315aa8c1a230575e756037f7. Messages this early contain only ASCII bytes; if we removed the CurrentMemoryContext requirement, the ensuing conversions would have no effect. Back-patch to 9.3 (all supported versions). Takayuki Tsunakawa, reviewed by Michael Paquier. Discussion: https://postgr.es/m/0A3221C70F24FB45833433255569204D1F80CC73@G01JPEXMBYT05 --- src/backend/utils/error/elog.c | 5 +++++ src/backend/utils/mb/mbutils.c | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c index 918db0a8f2d..d10a658e3d2 100644 --- a/src/backend/utils/error/elog.c +++ b/src/backend/utils/error/elog.c @@ -2117,10 +2117,15 @@ write_eventlog(int level, const char *line, int len) * try to convert the message to UTF16 and write it with ReportEventW(). * Fall back on ReportEventA() if conversion failed. * + * Since we palloc the structure required for conversion, also fall + * through to writing unconverted if we have not yet set up + * CurrentMemoryContext. + * * Also verify that we are not on our way into error recursion trouble due * to error messages thrown deep inside pgwin32_message_to_UTF16(). */ if (!in_error_recursion_trouble() && + CurrentMemoryContext != NULL && GetMessageEncoding() != GetACPEncoding()) { utf16 = pgwin32_message_to_UTF16(line, len, NULL); diff --git a/src/backend/utils/mb/mbutils.c b/src/backend/utils/mb/mbutils.c index ca7f129ebe1..e6f9151574d 100644 --- a/src/backend/utils/mb/mbutils.c +++ b/src/backend/utils/mb/mbutils.c @@ -1049,8 +1049,10 @@ GetMessageEncoding(void) #ifdef WIN32 /* - * Result is palloc'ed null-terminated utf16 string. The character length - * is also passed to utf16len if not null. Returns NULL iff failed. + * Convert from MessageEncoding to a palloc'ed, null-terminated utf16 + * string. The character length is also passed to utf16len if not + * null. Returns NULL iff failed. Before MessageEncoding initialization, "str" + * should be ASCII-only; this will function as though MessageEncoding is UTF8. */ WCHAR * pgwin32_message_to_UTF16(const char *str, int len, int *utf16len) -- GitLab