Skip to content

Commit 19cf9e9

Browse files
committed
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 5735efe. 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
1 parent ae5489e commit 19cf9e9

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/backend/utils/error/elog.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,10 +2041,15 @@ write_eventlog(int level, const char *line, int len)
20412041
* try to convert the message to UTF16 and write it with ReportEventW().
20422042
* Fall back on ReportEventA() if conversion failed.
20432043
*
2044+
* Since we palloc the structure required for conversion, also fall
2045+
* through to writing unconverted if we have not yet set up
2046+
* CurrentMemoryContext.
2047+
*
20442048
* Also verify that we are not on our way into error recursion trouble due
20452049
* to error messages thrown deep inside pgwin32_message_to_UTF16().
20462050
*/
20472051
if (!in_error_recursion_trouble() &&
2052+
CurrentMemoryContext != NULL &&
20482053
GetMessageEncoding() != GetACPEncoding())
20492054
{
20502055
utf16 = pgwin32_message_to_UTF16(line, len, NULL);

src/backend/utils/mb/mbutils.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,8 +1049,10 @@ GetMessageEncoding(void)
10491049

10501050
#ifdef WIN32
10511051
/*
1052-
* Result is palloc'ed null-terminated utf16 string. The character length
1053-
* is also passed to utf16len if not null. Returns NULL iff failed.
1052+
* Convert from MessageEncoding to a palloc'ed, null-terminated utf16
1053+
* string. The character length is also passed to utf16len if not
1054+
* null. Returns NULL iff failed. Before MessageEncoding initialization, "str"
1055+
* should be ASCII-only; this will function as though MessageEncoding is UTF8.
10541056
*/
10551057
WCHAR *
10561058
pgwin32_message_to_UTF16(const char *str, int len, int *utf16len)

0 commit comments

Comments
 (0)