Repository: microsoft/terminal (language: cpp)

Issue:
Exception 0xc0000409 in ucrtbase.dll
### Windows Terminal version

1.12.10983.0

### Windows build number

10.0.22000.708

### Other Software

_No response_

### Steps to reproduce

From the context menu it works, launching directly from the menu also works. But on the latest Windows 11 it doesn't work: Start -> right-click -> Windows Terminal or with Admin.

In the event viewer it's showing this:

> Faulting application name: WindowsTerminal.exe, version: 1.12.2204.8003, time stamp: 0x62508e2a
> Faulting module name: ucrtbase.dll, version: 10.0.22000.1, time stamp: 0x00e78ce9
> Exception code: 0xc0000409
> Fault offset: 0x000000000007dd7e
> Faulting process id: 0x228c
> Faulting application start time: 0x01d871efed6469ee
> Faulting application path: C:\Program Files\WindowsApps\Microsoft.WindowsTerminal_1.12.10983.0_x64__8wekyb3d8bbwe\WindowsTerminal.exe
> Faulting module path: C:\WINDOWS\System32\ucrtbase.dll
> Report Id: b77f3deb-3000-4181-9d2c-5bfb3c0e1170
> Faulting package full name: 
> Faulting package-relative application ID:

![image](https://user-images.githubusercontent.com/2883688/170762757-440b5550-4b3a-4364-a0da-5d671f921b24.png)


### Expected Behavior

The Windows Terminal should normally open.

### Actual Behavior

The Windows Terminal doesn't launch.


Discussion / hints:
I seem to have the same issue with my PC.

It all started with a Windows Update to Build 22000.613

While opening by search Terminal from start menu it works. 
This is likely MSFT-33471786. It occurs because we're allocating (for example) `0x1fffffffffffff30` bytes of memory.
I have a theory why this happens but I can't reproduce the crash so I can't debug it unfortunately.
If someone has the time to spare could you please do these two things for me?

If you're uncomfortable uploading these files to GitHub, please feel free to send me them via mail to:
`<my github username>@microsoft.com`

* A copy of your `settings.json` which you can find in this directory:
  `%LOCALAPPDATA%\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState`
* A backup of your Console's registry
  * Start regedit.exe
  * Navigate to `HKEY_CURRENT_USER\Console` and export the `Console` directory as a .reg file

![image](https://user-images.githubusercontent.com/2256941/171064862-5671d8c7-d0d8-4f7a-bb44-88102db4c988.png)
@lhecker 

Thank you for looking into this, here you go the files requested.
[Console.reg.txt](https://github.com/microsoft/terminal/files/8801013/Console.reg.txt)
[settings.json.txt](https://github.com/microsoft/terminal/files/8801014/settings.json.txt)

In my Event Viewer, the crash is detailed like this

Faulting application name: WindowsTerminal.exe, version: 1.12.2204.8003, time stamp: 0x62508e2a
Faulting module name: ucrtbase.dll, version: 10.0.22000.1, time stamp: 0x00e78ce9
Exception code: 0xc0000409
Fault offset: 0x000000000007dd7e
Faulting process ID: 0x3910
Faulting application start time: 0x01d874b9caf953c6
Faulting application path: C:\Program Files\WindowsApps\Microsoft.WindowsTerminal_1.12.10983.0_x64__8wekyb3d8bbwe\WindowsTerminal.exe
Faulting module path: C:\WINDOWS\System32\ucrtbase.dll
Report ID: 30a1bc3f-f972-455b-b2fb-f234abbcbc57
Faulting package full name: 
Faulting package-relative application ID: 

Fix patch:
diff --git a/src/host/screenInfo.cpp b/src/host/screenInfo.cpp
index a21086fb84e..af96a392cc0 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.ToInclusive() };
+    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<SHORT>(std::clamp(viewportRect.width(), 1, coordScreenBufferSize.width));
+    const auto cy = gsl::narrow_cast<SHORT>(std::clamp(viewportRect.height(), 1, coordScreenBufferSize.height));
+    const auto x = gsl::narrow_cast<SHORT>(std::clamp(viewportRect.left, 0, coordScreenBufferSize.width - cx));
+    const auto y = gsl::narrow_cast<SHORT>(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();


Test patch: