Skip to content

Commit a315fd5

Browse files
j4jamesDHowett
authored andcommitted
Correct horizontal coordinates in viewport overflow test (#8456)
When resizing the buffer in the `SetConsoleScreenBufferSize` and `SetConsoleScreenBufferInfoEx` APIs, we have tests in place to make sure that the resize doesn't result in the viewport extending past the bottom or right of the buffer (since that can eventually result in exceptions being thrown). Unfortunately these tests were using the wrong X coordinate, so they failed to detect an overflow along the horizontal axis. This PR corrects that mistake. PR #8309 was where the overflow detection was initially added. The original code was using the `Viewport::EndExclusive` method to determine the extent of the viewport, mistakenly thinking that it returned the bottom right coordinates (it actually returns the left coordinate). So I've now added a `BottomRightExclusive` method to the `Viewport` class, that actually does return the coordinates we need, and have updated the overflow tests to use that method instead. ## Validation Steps Performed I've manually confirmed that the test case is issue #8453 is no longer throwing an exception. Closes #8453 (cherry picked from commit 2a2f6b3)
1 parent 305946c commit a315fd5

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

src/host/getset.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ void ApiRoutines::GetLargestConsoleWindowSizeImpl(const SCREEN_INFORMATION& cont
528528
}
529529

530530
// Make sure the viewport doesn't now overflow the buffer dimensions.
531-
auto overflow = screenInfo.GetViewport().EndExclusive() - screenInfo.GetBufferSize().Dimensions();
531+
auto overflow = screenInfo.GetViewport().BottomRightExclusive() - screenInfo.GetBufferSize().Dimensions();
532532
if (overflow.X > 0 || overflow.Y > 0)
533533
{
534534
overflow = { std::max<SHORT>(overflow.X, 0), std::max<SHORT>(overflow.Y, 0) };
@@ -638,7 +638,7 @@ void ApiRoutines::GetLargestConsoleWindowSizeImpl(const SCREEN_INFORMATION& cont
638638
// Note that it also doesn't set cursor position.
639639

640640
// However, we do need to make sure the viewport doesn't now overflow the buffer dimensions.
641-
auto overflow = context.GetViewport().EndExclusive() - context.GetBufferSize().Dimensions();
641+
auto overflow = context.GetViewport().BottomRightExclusive() - context.GetBufferSize().Dimensions();
642642
if (overflow.X > 0 || overflow.Y > 0)
643643
{
644644
overflow = { std::max<SHORT>(overflow.X, 0), std::max<SHORT>(overflow.Y, 0) };

src/types/inc/viewport.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ namespace Microsoft::Console::Types
5757
SHORT Height() const noexcept;
5858
SHORT Width() const noexcept;
5959
COORD Origin() const noexcept;
60+
COORD BottomRightExclusive() const noexcept;
6061
COORD EndExclusive() const noexcept;
6162
COORD Dimensions() const noexcept;
6263

src/types/viewport.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ COORD Viewport::Origin() const noexcept
137137
return { Left(), Top() };
138138
}
139139

140+
// Method Description:
141+
// - Get a coord representing the bottom right of the viewport in exclusive terms.
142+
// Arguments:
143+
// - <none>
144+
// Return Value:
145+
// - the exclusive bottom right coordinates of this viewport.
146+
COORD Viewport::BottomRightExclusive() const noexcept
147+
{
148+
return { RightExclusive(), BottomExclusive() };
149+
}
150+
140151
// Method Description:
141152
// - For Accessibility, get a COORD representing the end of this viewport in exclusive terms.
142153
// - This is needed to represent an exclusive endpoint in UiaTextRange that includes the last

0 commit comments

Comments
 (0)