Changeset 3547 for trunk/src/lib/maybe_con_write.c
- Timestamp:
- Jan 29, 2022, 3:39:47 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/maybe_con_write.c
r3188 r3547 63 63 * call WriteConsoleW directly. 64 64 */ 65 if (cbToWrite > 0 )65 if (cbToWrite > 0 && cbToWrite < INT_MAX / 2) 66 66 { 67 67 HANDLE hCon = (HANDLE)_get_osfhandle(fd); … … 71 71 if (is_console_handle((intptr_t)hCon)) 72 72 { 73 size_t cwcTmp = cbToWrite * 2 + 16; 74 wchar_t *pawcTmp = (wchar_t *)malloc(cwcTmp * sizeof(wchar_t)); 75 if (pawcTmp) 73 wchar_t awcBuf[1024]; 74 wchar_t *pawcBuf; 75 wchar_t *pawcBufFree = NULL; 76 size_t cwcBuf = cbToWrite * 2 + 16; 77 if (cwcBuf < sizeof(awcBuf) / sizeof(awcBuf[0])) 76 78 { 77 int cwcToWrite; 78 static UINT s_uConsoleCp = 0; 79 if (s_uConsoleCp == 0) 80 s_uConsoleCp = GetConsoleCP(); 81 82 cwcToWrite = MultiByteToWideChar(s_uConsoleCp, 0 /*dwFlags*/, pvBuf, (int)cbToWrite, 83 pawcTmp, (int)(cwcTmp - 1)); 79 pawcBuf = awcBuf; 80 cwcBuf = sizeof(awcBuf) / sizeof(awcBuf[0]); 81 } 82 else 83 pawcBufFree = pawcBuf = (wchar_t *)malloc(cwcBuf * sizeof(wchar_t)); 84 if (pawcBuf) 85 { 86 int cwcToWrite = MultiByteToWideChar(get_crt_codepage(), 0 /*dwFlags*/, 87 pvBuf, (int)cbToWrite, 88 pawcBuf, (int)(cwcBuf - 1)); 84 89 if (cwcToWrite > 0) 85 90 { 91 int rc; 92 pawcBuf[cwcToWrite] = '\0'; 93 86 94 /* Let the CRT do the rest. At least the Visual C++ 2010 CRT 87 95 sources indicates _cputws will do the right thing. */ 88 pawcTmp[cwcToWrite] = '\0'; 89 if (_cputws(pawcTmp) >= 0) 96 rc = _cputws(pawcBuf); 97 if (pawcBufFree) 98 free(pawcBufFree); 99 if (rc >= 0) 90 100 return cbToWrite; 91 101 return -1; 92 102 } 103 free(pawcBufFree); 93 104 } 94 105 }
Note:
See TracChangeset
for help on using the changeset viewer.