From 02ab51d75476a4e6241d174fe4eefabac14cfb8c Mon Sep 17 00:00:00 2001 From: Leonard Hecker Date: Wed, 1 Jun 2022 01:45:48 +0200 Subject: [PATCH 1/2] Fix SetConsoleWindowInfo being able to crash ConPTY --- src/host/screenInfo.cpp | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/host/screenInfo.cpp b/src/host/screenInfo.cpp index a21086fb84e..2d96e1f0bf3 100644 --- a/src/host/screenInfo.cpp +++ b/src/host/screenInfo.cpp @@ -2268,30 +2268,19 @@ void SCREEN_INFORMATION::SetViewport(const Viewport& newViewport, } // do adjustments on a copy that's easily manipulated. - auto srCorrected = newViewport.ToExclusive(); + const til::rect viewportRect{ newViewport.ToExclusive() }; + const til::size coordScreenBufferSize{ GetBufferSize().Dimensions() }; - if (srCorrected.Left < 0) - { - srCorrected.Right -= srCorrected.Left; - srCorrected.Left = 0; - } - if (srCorrected.Top < 0) - { - srCorrected.Bottom -= srCorrected.Top; - srCorrected.Top = 0; - } + // MSFT-33471786, GH#13193: + // newViewport may reside anywhere outside of the valid coordScreenBufferSize. + // For instance it might be scrolled down more than our text buffer allows to be scrolled. + const auto cx = gsl::narrow_cast(std::clamp(viewportRect.width(), 1, coordScreenBufferSize.width)); + const auto cy = gsl::narrow_cast(std::clamp(viewportRect.height(), 1, coordScreenBufferSize.height)); + const auto x = gsl::narrow_cast(std::clamp(viewportRect.left, 0, coordScreenBufferSize.width - cx)); + const auto y = gsl::narrow_cast(std::clamp(viewportRect.top, 0, coordScreenBufferSize.height - cy)); - const auto coordScreenBufferSize = GetBufferSize().Dimensions(); - if (srCorrected.Right > coordScreenBufferSize.X) - { - srCorrected.Right = coordScreenBufferSize.X; - } - if (srCorrected.Bottom > coordScreenBufferSize.Y) - { - srCorrected.Bottom = coordScreenBufferSize.Y; - } + _viewport = Viewport::FromExclusive({ x, y, x + cx, y + cy }); - _viewport = Viewport::FromExclusive(srCorrected); if (updateBottom) { UpdateBottom(); From 84ccf12be44fce2493e208d0dacd772faa134b7d Mon Sep 17 00:00:00 2001 From: Leonard Hecker Date: Wed, 1 Jun 2022 03:25:36 +0200 Subject: [PATCH 2/2] Fix confusion between inclusive/exclusive SMALL_RECT --- src/host/screenInfo.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/host/screenInfo.cpp b/src/host/screenInfo.cpp index 2d96e1f0bf3..af96a392cc0 100644 --- a/src/host/screenInfo.cpp +++ b/src/host/screenInfo.cpp @@ -2268,7 +2268,7 @@ void SCREEN_INFORMATION::SetViewport(const Viewport& newViewport, } // do adjustments on a copy that's easily manipulated. - const til::rect viewportRect{ newViewport.ToExclusive() }; + const til::rect viewportRect{ newViewport.ToInclusive() }; const til::size coordScreenBufferSize{ GetBufferSize().Dimensions() }; // MSFT-33471786, GH#13193: