More conhost exceptions via SetConsoleScreenBufferSize #11645

Closed
opened 2026-01-31 02:53:31 +00:00 by claunia · 1 comment
Owner

Originally created by @j4james on GitHub (Dec 1, 2020).

Environment

Windows build number: Version 10.0.18363.1198
Windows Terminal version (if applicable): Commit 62131720aa

Steps to reproduce

  1. Start a conhost cmd shell.
  2. Open the properties and make sure Wrap text output on resize is unchecked.
  3. Compile and run the program below.
#include <windows.h>
int main()
{
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD coord;
	coord.X = 160;
	coord.Y = 100;
	SetConsoleScreenBufferSize(h, coord);
	SMALL_RECT r = { 80, 75, 159, 99 };
	SetConsoleWindowInfo(h, TRUE, &r);
	SetConsoleCursorPosition(h, { 159, 99 });
	coord.X = 80;
	SetConsoleScreenBufferSize(h, coord);
	return 0;
}

Expected behavior

The conhost instance shouldn't crash and there shouldn't be any exceptions logged in the debug output.

Actual behavior

On my current version of Windows the conhost instance crashes. On the latest version of OpenConsole the exception is caught, so it at least doesn't crash, but you can see the error being logged in the debug output.

This is essentially the same bug as #1976, only the overflow is horizontal rather than vertical. It should have been fixed by PR #8309, but I managed to screw that up. I mistakenly thought the Viewport::EndExclusive method returned the bottom right extent of the viewport, but it's actually returning { Left(), BottomExclusive() }, so the overflow calculation doesn't detect a horizontal overflow. See here:

d09fdd61cb/src/host/getset.cpp (L530-L531)

and here:

ae550e0969/src/types/viewport.cpp (L148-L151)
The fix is easy enough. I'd probably just add something like an ExclusiveExtent method to the Viewport class that returns the range I was actually excepting to get from Viewport::EndExclusive. Does that seem reasonable?

Originally created by @j4james on GitHub (Dec 1, 2020). # Environment Windows build number: Version 10.0.18363.1198 Windows Terminal version (if applicable): Commit 62131720aa7a95df7ed397801c373d4eab0fd3d6 # Steps to reproduce 1. Start a conhost cmd shell. 2. Open the properties and make sure _Wrap text output on resize_ is unchecked. 3. Compile and run the program below. ```c #include <windows.h> int main() { HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); COORD coord; coord.X = 160; coord.Y = 100; SetConsoleScreenBufferSize(h, coord); SMALL_RECT r = { 80, 75, 159, 99 }; SetConsoleWindowInfo(h, TRUE, &r); SetConsoleCursorPosition(h, { 159, 99 }); coord.X = 80; SetConsoleScreenBufferSize(h, coord); return 0; } ``` # Expected behavior The conhost instance shouldn't crash and there shouldn't be any exceptions logged in the debug output. # Actual behavior On my current version of Windows the conhost instance crashes. On the latest version of OpenConsole the exception is caught, so it at least doesn't crash, but you can see the error being logged in the debug output. This is essentially the same bug as #1976, only the overflow is horizontal rather than vertical. It should have been fixed by PR #8309, but I managed to screw that up. I mistakenly thought the `Viewport::EndExclusive` method returned the bottom right extent of the viewport, but it's actually returning `{ Left(), BottomExclusive() }`, so the overflow calculation doesn't detect a horizontal overflow. See here: https://github.com/microsoft/terminal/blob/d09fdd61cbb11b7ef2ccdd4820349ffe898ad583/src/host/getset.cpp#L530-L531 and here: https://github.com/microsoft/terminal/blob/ae550e0969595f062b15c2ff5cc33d4afe8ebc3f/src/types/viewport.cpp#L148-L151 The fix is easy enough. I'd probably just add something like an `ExclusiveExtent` method to the `Viewport` class that returns the range I was actually excepting to get from `Viewport::EndExclusive`. Does that seem reasonable?
Author
Owner

@zadjii-msft commented on GitHub (Dec 2, 2020):

Yea that seems sensible to me. Thanks for investigating and putting the fix together!

@zadjii-msft commented on GitHub (Dec 2, 2020): Yea that seems sensible to me. Thanks for investigating and putting the fix together!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#11645