Refactor: Use TryParse in view models for safer numeric input parsing (#14323)
* Refactor: Use TryParse in view models for safer numeric input parsing Replaces parsing logic with TryParse across view models to prevent overflow/format crashes from invalid user input. Specifically fixes the VM Idle Timeout crash reported in #14312, while improving stability for other numeric settings. * Fix crash when opening Optional Features settings page
Dimitris Chatzis committed
Mar 2, 2026 at 20:14 UTC
26bf49db1e7e4a96577bda435ad17b26365a8b8c
5 files changed
+48
-7
src/windows/wslsettings/ViewModels/Settings/FileSystemViewModel.cs
+8
-1
@@ -38,7 +38,14 @@ public partial class FileSystemViewModel : WslConfigSettingViewModel
38
{
39
if (ValidateInput(value, Constants.WholeNumberRegex))
40
{
41
- Set(ref _defaultVHDSize!, Convert.ToUInt64(value));
41
+ if (UInt64.TryParse(value, out ulong parsedValue))
42
+ {
43
+ Set(ref _defaultVHDSize!, parsedValue);
44
+ }
45
+ else
46
+ {
47
+ OnPropertyChanged();
48
+ }
49
}
50
}
51
}
src/windows/wslsettings/ViewModels/Settings/MemAndProcViewModel.cs
+24
-3
@@ -52,7 +52,14 @@ public partial class MemAndProcViewModel : WslConfigSettingViewModel
52
{
53
if (ValidateInput(value, Constants.IntegerRegex))
54
{
55
- Set(ref _procCount!, Convert.ToInt32(value));
55
+ if (Int32.TryParse(value, out int parsedValue))
56
+ {
57
+ Set(ref _procCount!, parsedValue);
58
+ }
59
+ else
60
+ {
61
+ OnPropertyChanged();
62
+ }
63
}
64
}
65
}
@@ -92,7 +99,14 @@ public partial class MemAndProcViewModel : WslConfigSettingViewModel
99
{
100
if (ValidateInput(value, Constants.WholeNumberRegex))
101
{
95
- Set(ref _memorySize!, Convert.ToUInt64(value));
102
+ if (UInt64.TryParse(value, out ulong parsedValue))
103
+ {
104
+ Set(ref _memorySize!, parsedValue);
105
+ }
106
+ else
107
+ {
108
+ OnPropertyChanged();
109
+ }
110
}
111
}
112
}
@@ -132,7 +146,14 @@ public partial class MemAndProcViewModel : WslConfigSettingViewModel
146
{
147
if (ValidateInput(value, Constants.WholeNumberRegex))
148
{
135
- Set(ref _swapSize!, Convert.ToUInt64(value));
149
+ if (UInt64.TryParse(value, out ulong parsedValue))
150
+ {
151
+ Set(ref _swapSize!, parsedValue);
152
+ }
153
+ else
154
+ {
155
+ OnPropertyChanged();
156
+ }
157
}
158
}
159
}
src/windows/wslsettings/ViewModels/Settings/NetworkingViewModel.cs
+8
-1
@@ -136,7 +136,14 @@ public partial class NetworkingViewModel : WslConfigSettingViewModel
136
{
137
if (ValidateInput(value, Constants.IntegerRegex))
138
{
139
- Set(ref _initialAutoProxyTimeout!, Convert.ToInt32(value));
139
+ if (Int32.TryParse(value, out int parsedValue))
140
+ {
141
+ Set(ref _initialAutoProxyTimeout!, parsedValue);
142
+ }
143
+ else
144
+ {
145
+ OnPropertyChanged();
146
+ }
147
}
148
}
149
}
src/windows/wslsettings/ViewModels/Settings/OptionalFeaturesViewModel.cs
+8
-1
@@ -82,7 +82,14 @@ public partial class OptionalFeaturesViewModel : WslConfigSettingViewModel
82
{
83
if (ValidateInput(value, Constants.IntegerRegex))
84
{
85
- Set(ref _vMIdleTimeout!, Convert.ToInt32(value));
85
+ if (Int32.TryParse(value, out int parsedValue))
86
+ {
87
+ Set(ref _vMIdleTimeout!, parsedValue);
88
+ }
89
+ else
90
+ {
91
+ OnPropertyChanged();
92
+ }
93
}
94
}
95
}
src/windows/wslsettings/Views/Settings/OptionalFeaturesPage.xaml.cs
-1
@@ -34,7 +34,6 @@ public sealed partial class OptionalFeaturesPage : Page
34
private void OnPageLoaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
35
{
36
OptionalFeaturesPageRoot.Focus(FocusState.Programmatic);
37
- RuntimeHelper.SetupExpanderFocusManagementByName(this, "SystemdSettingsExpander", "InitTextBox");
37
RuntimeHelper.SetupExpanderFocusManagementByName(this, "VMIdleTimeoutExpander", "VMIdleTimeoutTextBox");
38
}
39