Skip to content

Refactor: Use TryParse in view models for safer numeric input parsing#14323

Merged
benhillis merged 2 commits intomicrosoft:masterfrom
dhtzs:fix/input-parsing-tryparse
Mar 2, 2026
Merged

Refactor: Use TryParse in view models for safer numeric input parsing#14323
benhillis merged 2 commits intomicrosoft:masterfrom
dhtzs:fix/input-parsing-tryparse

Conversation

@dhtzs
Copy link
Copy Markdown
Contributor

@dhtzs dhtzs commented Feb 28, 2026

Summary of the Pull Request

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. Also removes an orphaned reference to SystemdSettingsExpander in OptionalFeaturesPage.xaml.cs which caused a Debug.Assert failure and crashed the application when the Optional Features page was opened. This expander is no longer present in the XAML markup.

PR Checklist

  • Closes: WSL Settings crash after putting too large number in VM Idle Timeout #14312
  • Communication: I've discussed this with core contributors already. If work hasn't been agreed, this work might be rejected
  • Tests: Added/updated if needed and all pass
  • Localization: All end user facing strings can be localized
  • Dev docs: Added/updated if needed
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

Validation Steps Performed

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 microsoft#14312, while improving stability for other numeric settings.
Copilot AI review requested due to automatic review settings February 28, 2026 21:37
@dhtzs dhtzs requested a review from a team as a code owner February 28, 2026 21:37
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens numeric settings input handling in the WSL Settings app by replacing exception-throwing numeric conversions with TryParse in several settings view models, preventing crashes from overflow/invalid user input (notably the VM Idle Timeout crash in #14312).

Changes:

  • Replace Convert.ToInt32/ToUInt64 with Int32.TryParse / UInt64.TryParse in multiple numeric-setting setters.
  • On parse failure (e.g., overflow), revert the UI by raising OnPropertyChanged() instead of crashing.
  • Apply the same safer parsing pattern to optional features, networking, memory/processor, and filesystem settings.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/windows/wslsettings/ViewModels/Settings/OptionalFeaturesViewModel.cs Uses Int32.TryParse for VM Idle Timeout to avoid overflow crashes and revert invalid input.
src/windows/wslsettings/ViewModels/Settings/NetworkingViewModel.cs Uses Int32.TryParse for Initial Auto Proxy Timeout to prevent invalid input crashes.
src/windows/wslsettings/ViewModels/Settings/MemAndProcViewModel.cs Uses TryParse for processor count, memory size, and swap size numeric inputs to prevent exceptions.
src/windows/wslsettings/ViewModels/Settings/FileSystemViewModel.cs Uses UInt64.TryParse for Default VHD Size to avoid invalid/overflow conversion crashes.

@benhillis
Copy link
Copy Markdown
Member

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Comment thread src/windows/wslsettings/ViewModels/Settings/FileSystemViewModel.cs
@benhillis benhillis merged commit 26bf49d into microsoft:master Mar 2, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants