Skip to content

Commit c70ad6a

Browse files
committed
8352906: stdout/err.encoding on Windows set by incorrect Win32 call
Reviewed-by: bpb, alanb
1 parent da3bb06 commit c70ad6a

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/java.base/windows/native/libjava/java_props_md.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -45,7 +45,7 @@
4545
#endif
4646

4747
typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
48-
static boolean SetupI18nProps(LCID lcid, char** language, char** script, char** country,
48+
static BOOL SetupI18nProps(LCID lcid, char** language, char** script, char** country,
4949
char** variant, char** encoding);
5050

5151
#define PROPSIZE 9 // eight-letter + null terminator
@@ -128,21 +128,30 @@ getEncodingInternal(LCID lcid)
128128
return ret;
129129
}
130130

131-
static char* getConsoleEncoding()
131+
static char* getConsoleEncoding(BOOL output)
132132
{
133133
size_t buflen = 16;
134134
char* buf = malloc(buflen);
135135
int cp;
136136
if (buf == NULL) {
137137
return NULL;
138138
}
139-
cp = GetConsoleCP();
140-
if (cp >= 874 && cp <= 950)
139+
if (output) {
140+
cp = GetConsoleOutputCP();
141+
} else {
142+
cp = GetConsoleCP();
143+
}
144+
if (cp >= 874 && cp <= 950) {
141145
snprintf(buf, buflen, "ms%d", cp);
142-
else if (cp == 65001)
146+
} else if (cp == 65001) {
143147
snprintf(buf, buflen, "UTF-8");
144-
else
148+
} else if (cp == 0) {
149+
// Failed to get the console code page
150+
free(buf);
151+
buf = NULL;
152+
} else {
145153
snprintf(buf, buflen, "cp%d", cp);
154+
}
146155
return buf;
147156
}
148157

@@ -221,7 +230,7 @@ getHomeFromShell32()
221230
return u_path;
222231
}
223232

224-
static boolean
233+
static BOOL
225234
haveMMX(void)
226235
{
227236
return IsProcessorFeaturePresent(PF_MMX_INSTRUCTIONS_AVAILABLE);
@@ -251,7 +260,7 @@ cpu_isalist(void)
251260
return NULL;
252261
}
253262

254-
static boolean
263+
static BOOL
255264
SetupI18nProps(LCID lcid, char** language, char** script, char** country,
256265
char** variant, char** encoding) {
257266
/* script */
@@ -343,8 +352,8 @@ GetJavaProperties(JNIEnv* env)
343352
/* OS properties */
344353
{
345354
char buf[100];
346-
boolean is_workstation;
347-
boolean is_64bit;
355+
BOOL is_workstation;
356+
BOOL is_64bit;
348357
DWORD platformId;
349358
{
350359
OSVERSIONINFOEX ver;
@@ -677,15 +686,15 @@ GetJavaProperties(JNIEnv* env)
677686
hStdOutErr = GetStdHandle(STD_OUTPUT_HANDLE);
678687
if (hStdOutErr != INVALID_HANDLE_VALUE &&
679688
GetFileType(hStdOutErr) == FILE_TYPE_CHAR) {
680-
sprops.stdout_encoding = getConsoleEncoding();
689+
sprops.stdout_encoding = getConsoleEncoding(TRUE);
681690
}
682691
hStdOutErr = GetStdHandle(STD_ERROR_HANDLE);
683692
if (hStdOutErr != INVALID_HANDLE_VALUE &&
684693
GetFileType(hStdOutErr) == FILE_TYPE_CHAR) {
685694
if (sprops.stdout_encoding != NULL)
686695
sprops.stderr_encoding = sprops.stdout_encoding;
687696
else
688-
sprops.stderr_encoding = getConsoleEncoding();
697+
sprops.stderr_encoding = getConsoleEncoding(TRUE);
689698
}
690699
}
691700
}

0 commit comments

Comments
 (0)