Repository: microsoft/terminal (language: cpp)

Issue:
Launch size doesn't have a sensible max value
### Windows Terminal version

1.11.2921.0

### Windows build number

10.0.22000.376

### Other Software

_No response_

### Steps to reproduce

Setting the max columns or rows visible on start to a crazy value like 900000000 doesn't make any sense. I also got an error.

### Expected Behavior

_No response_

### Actual Behavior

![](https://user-images.githubusercontent.com/62265474/146324749-4f648a82-d686-4e48-ace8-98d3d23716de.png)


Discussion / hints:
That's a good point, thanks!
@zadjii-msft Mind if I take my shot at this? Would probably just add a max value to the NumberBoxes and also clamp the read sizes from the settings file to something like 1000
@marcelwgn you're always free to just take a shot at anything around these parts 😝 

Technically, there's code somewhere in screenInfo.cpp that conhost uses to try and figure out the max size of a window for the display it's currently on. But I think using that here would probably be overkill. I'm sure 1000 columns is more than enough, even for like a 8k ultrawide or something ridiculous. 

Fix patch:
diff --git a/src/cascadia/TerminalSettingsEditor/Launch.xaml b/src/cascadia/TerminalSettingsEditor/Launch.xaml
index e68076a5032..2498662a4ce 100644
--- a/src/cascadia/TerminalSettingsEditor/Launch.xaml
+++ b/src/cascadia/TerminalSettingsEditor/Launch.xaml
@@ -184,6 +184,8 @@
                                     Grid.Row="0"
                                     Grid.Column="1"
                                     VerticalAlignment="Center"
+                                    Maximum="999"
+                                    Minimum="1"
                                     Style="{StaticResource LaunchSizeNumberBoxStyle}"
                                     Value="{x:Bind ViewModel.InitialCols, Mode=TwoWay}" />
                     <TextBlock x:Uid="Globals_InitialRows"
@@ -195,6 +197,8 @@
                                     Grid.Row="1"
                                     Grid.Column="1"
                                     VerticalAlignment="Center"
+                                    Maximum="999"
+                                    Minimum="1"
                                     Style="{StaticResource LaunchSizeNumberBoxStyle}"
                                     Value="{x:Bind ViewModel.InitialRows, Mode=TwoWay}" />
                 </Grid>
diff --git a/src/cascadia/TerminalSettingsModel/GlobalAppSettings.cpp b/src/cascadia/TerminalSettingsModel/GlobalAppSettings.cpp
index c307050a998..ff6dd5eec63 100644
--- a/src/cascadia/TerminalSettingsModel/GlobalAppSettings.cpp
+++ b/src/cascadia/TerminalSettingsModel/GlobalAppSettings.cpp
@@ -137,6 +137,17 @@ void GlobalAppSettings::LayerJson(const Json::Value& json, const OriginTag origi
     MTSM_GLOBAL_SETTINGS(GLOBAL_SETTINGS_LAYER_JSON)
 #undef GLOBAL_SETTINGS_LAYER_JSON
 
+    // GH#11975 We only want to allow sensible values and prevent crashes, so we are clamping those values
+    // We only want to assign if the value did change through clamping,
+    // otherwise we could end up setting defaults that get persisted
+    if (this->HasInitialCols())
+    {
+        this->InitialCols(std::clamp(this->InitialCols(), 1, 999));
+    }
+    if (this->HasInitialRows())
+    {
+        this->InitialRows(std::clamp(this->InitialRows(), 1, 999));
+    }
     LayerActionsFrom(json, origin, true);
 
     JsonUtils::GetValueForKey(json, LegacyReloadEnvironmentVariablesKey, _legacyReloadEnvironmentVariables);
diff --git a/src/cascadia/UnitTests_SettingsModel/DeserializationTests.cpp b/src/cascadia/UnitTests_SettingsModel/DeserializationTests.cpp
index 2425e3421d8..bd17076bc94 100644
--- a/src/cascadia/UnitTests_SettingsModel/DeserializationTests.cpp
+++ b/src/cascadia/UnitTests_SettingsModel/DeserializationTests.cpp
@@ -47,6 +47,7 @@ namespace SettingsModelUnitTests
         TEST_METHOD(ValidateKeybindingsWarnings);
         TEST_METHOD(ValidateColorSchemeInCommands);
         TEST_METHOD(ValidateExecuteCommandlineWarning);
+        TEST_METHOD(TestClampingOfStartupColumnAndViewProperties);
         TEST_METHOD(TestTrailingCommas);
         TEST_METHOD(TestCommandsAndKeybindings);
         TEST_METHOD(TestNestedCommandWithoutName);
@@ -1301,6 +1302,20 @@ namespace SettingsModelUnitTests
         VERIFY_ARE_EQUAL(SettingsLoadWarnings::MissingRequiredParameter, settings->Warnings().GetAt(3));
     }
 
+    void DeserializationTests::TestClampingOfStartupColumnAndViewProperties()
+    {
+        static constexpr std::string_view inputSettings{ R"({
+            "initialCols" : 1000000,
+            "initialRows" : -1000000,
+            "profiles": [{ "name": "profile0" }]
+        })" };
+
+        const auto settings = createSettings(inputSettings);
+
+        VERIFY_ARE_EQUAL(999, settings->GlobalSettings().InitialCols());
+        VERIFY_ARE_EQUAL(1, settings->GlobalSettings().InitialRows());
+    }
+
     void DeserializationTests::TestTrailingCommas()
     {
         static constexpr std::string_view badSettings{ R"({


Test patch: