Commit 8a8072e5 authored by Joshua Warner's avatar Joshua Warner

use KJ_STACK_ARRAY for win32 text buffers in main.c++

parent c0fbfd9c
...@@ -73,8 +73,8 @@ static void writeLineToFd(int fd, StringPtr message) { ...@@ -73,8 +73,8 @@ static void writeLineToFd(int fd, StringPtr message) {
} }
#if _WIN32 #if _WIN32
char* newlineExpansionBuffer = (char*)_alloca(2 * (message.size() + 1)); KJ_STACK_ARRAY(char, newlineExpansionBuffer, 2 * (message.size() + 1), 128, 512);
char* p = newlineExpansionBuffer; char* p = newlineExpansionBuffer.begin();
for(char ch : message) { for(char ch : message) {
if(ch == '\n') { if(ch == '\n') {
*(p++) = '\r'; *(p++) = '\r';
...@@ -86,9 +86,9 @@ static void writeLineToFd(int fd, StringPtr message) { ...@@ -86,9 +86,9 @@ static void writeLineToFd(int fd, StringPtr message) {
*(p++) = '\n'; *(p++) = '\n';
} }
size_t newlineExpandedSize = p - newlineExpansionBuffer; size_t newlineExpandedSize = p - newlineExpansionBuffer.begin();
KJ_ASSERT(newlineExpandedSize <= 2 * (message.size() + 1)); KJ_ASSERT(newlineExpandedSize <= newlineExpansionBuffer.size());
HANDLE handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd)); HANDLE handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
DWORD consoleMode; DWORD consoleMode;
...@@ -96,21 +96,21 @@ static void writeLineToFd(int fd, StringPtr message) { ...@@ -96,21 +96,21 @@ static void writeLineToFd(int fd, StringPtr message) {
DWORD writtenSize; DWORD writtenSize;
if(redirectedToFile) { if(redirectedToFile) {
WriteFile(handle, newlineExpansionBuffer, newlineExpandedSize, &writtenSize, nullptr); WriteFile(handle, newlineExpansionBuffer.begin(), newlineExpandedSize, &writtenSize, nullptr);
} else { } else {
wchar_t* buffer = (wchar_t*)_alloca(sizeof(wchar_t) * newlineExpandedSize); KJ_STACK_ARRAY(wchar_t, buffer, newlineExpandedSize, 128, 512);
size_t finalSize = MultiByteToWideChar( size_t finalSize = MultiByteToWideChar(
CP_UTF8, CP_UTF8,
0, 0,
newlineExpansionBuffer, newlineExpansionBuffer.begin(),
newlineExpandedSize, newlineExpandedSize,
buffer, buffer.begin(),
newlineExpandedSize); buffer.size());
KJ_ASSERT(finalSize <= newlineExpandedSize); KJ_ASSERT(finalSize <= buffer.size());
WriteConsoleW(handle, buffer, finalSize, &writtenSize, nullptr); WriteConsoleW(handle, buffer.begin(), finalSize, &writtenSize, nullptr);
} }
#else #else
// Unfortunately the writev interface requires non-const pointers even though it won't modify // Unfortunately the writev interface requires non-const pointers even though it won't modify
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment