Skip to content

Commit

Permalink
Bazel client, Windows: fix console handling bugs
Browse files Browse the repository at this point in the history
Fix 3 bugs in blaze_util_windows:
- off-by-one error in the loop ensuring the std
handles are all open
- don't auto-close stdout's HANDLE after querying
the console window's size
- error-handling for when stdout is redirected
thus ::GetStdHandle returns an invalid handle

See bazelbuild#2107

--
PiperOrigin-RevId: 150310578
MOS_MIGRATED_REVID=150310578
  • Loading branch information
laszlocsomor authored and meteorcloudy committed Mar 16, 2017
1 parent 8a104fa commit 3d97f49
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/main/cpp/blaze_util_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,7 @@ void SetupStdStreams() {
#ifdef COMPILER_MSVC
static const DWORD stdhandles[] = {STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
STD_ERROR_HANDLE};
for (int i = 0; i < 2; ++i) {
for (int i = 0; i <= 2; ++i) {
HANDLE handle = ::GetStdHandle(stdhandles[i]);
if (handle == INVALID_HANDLE_VALUE || handle == NULL) {
// Ensure we have open fds to each std* stream. Otherwise we can end up
Expand Down Expand Up @@ -1346,18 +1346,17 @@ int GetTerminalColumns() {
}
}

#ifdef COMPILER_MSVC
// This code path is MSVC-only because when running under MSYS there's no
// Windows console attached so GetConsoleScreenBufferInfo fails.
windows_util::AutoHandle stdout_handle(::GetStdHandle(STD_OUTPUT_HANDLE));
CONSOLE_SCREEN_BUFFER_INFO screen_info;
if (GetConsoleScreenBufferInfo(stdout_handle, &screen_info)) {
int width = 1 + screen_info.srWindow.Right - screen_info.srWindow.Left;
if (width > 1) {
return width;
HANDLE stdout_handle = ::GetStdHandle(STD_OUTPUT_HANDLE);
if (stdout_handle != INVALID_HANDLE_VALUE) {
// stdout_handle may be invalid when stdout is redirected.
CONSOLE_SCREEN_BUFFER_INFO screen_info;
if (GetConsoleScreenBufferInfo(stdout_handle, &screen_info)) {
int width = 1 + screen_info.srWindow.Right - screen_info.srWindow.Left;
if (width > 1) {
return width;
}
}
}
#endif // COMPILER_MSVC

return 80; // default if not a terminal.
}
Expand Down

0 comments on commit 3d97f49

Please sign in to comment.