I forwarded the issue report I posted to sourceforge bug ticket
to this mailing list.
https://sourceforge.net/p/mingw-w64/bugs/943/
### Overview
With the following code, __mingw_printf() does not print non-ASCII
chars correctly while __ms_printf() does.
/* Save this source code in UTF-8 */
#include <stdio.h>
#include <locale.h>
int main()
{
const wchar_t *wstr = L"αβγδ";
setlocale(LC_CTYPE, "");
__ms_printf("%ls\n", wstr);
__mingw_printf("%ls\n", wstr);
return 0;
}
### Environment
* OS: Windows 10 Pro 64 bit (21H2)
* cygwin 3.3.5 + mingw64-x86_64-gcc-g++ 11.3.0-1
* msys2 3.3.5 + mingw64/mingw-w64-x86_64-gcc 12.1.0-2
* System locale: Japanese
* Default code page: 932 (Japanese)
### How to reproduce
1. Compile the test code above with x86_64-w64-mingw32-gcc.
2. Execute the a.exe file in command prompt.
### Expected result
αβγδ
αβγδ
### Actual result
Output of a.exe is:
αβγδ
ソタチツ
However, a.exe | more or a.exe > output.txt outputs non-ASCII chars
as expected with both __ms_printf() and __mingw_printf().
### Analysis
This seems to be a problem of msvcrt.dll. The following code also
fails in the same way.
#include <windows.h>
#include <stdio.h>
#include <locale.h>
int main()
{
char buf[] = {0x83, 0xbf, '\n', 0}; /* "α\n" in CP932 */
int (*__putchar)(int);
char *(*__setlocale)(int, char *);
HMODULE h = LoadLibrary("msvcrt.dll");
__setlocale = (char *(*)(int, char *))GetProcAddress(h, "setlocale");
__setlocale(LC_CTYPE, "");
__putchar = (int (*)(int))GetProcAddress(h, "putchar");
for (int i=0; i<sizeof(buf)-1; i++) __putchar(buf[i]);
return 0;
}
Resuted output:
ソ
Without setlocale() call, the probelm does not happen.
### Workaround
I found a workaround for this issue.
Adding
if (_isatty(_fileno(stdout))) _setmode(_fileno(stdout), _O_BINARY);
after setlocale() solves the issue.
Similally, _setmode() to _O_U8TEXT is effectove for wprintf() etc.
### Patch for this issue
I attached the PoC patch for this issue. The file name and its
location may not be appropriate.
I was worried about the overhead, but it seems to get faster with
this patch.
--
Takashi Yano <[email protected]>
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public