Unit converter reads out of bounds when the saved UnitConverterPreferences setting is malformed

Open Beginner friendly
#2,532 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
desktop

Research direction

Start in src/CalcManager/UnitConverter.cpp with RestoreUserPreferences, then read StringToUnit, StringToCategory and StringToVector, where the fixed-index reads of tokenList happen. Add a call with a malformed value such as ||| to CalculatorUnitTests/UnitConverterTest.cpp to reproduce the out-of-bounds read. Done when malformed preferences leave the converter at its defaults and the tests cover the round trip and several bad inputs.

Written by the indexing model from the issue text.

Description

Describe the bug
UnitConverter::RestoreUserPreferences (src/CalcManager/UnitConverter.cpp) only checks that the saved preferences string has three |-separated parts. StringToUnit and StringToCategory then read the ;-separated fields at fixed positions (tokenList[0] to tokenList[5]). The field count is checked only by an assert.

If the stored UnitConverterPreferences value is malformed (for example |||, or a part that is missing a field), those functions index past the end of the token list. With asserts enabled the app aborts; without them it is an out-of-bounds read. The setting is read every time a converter opens, so a bad value keeps causing the failure until the app's settings are reset.

I found this by reading the source. I haven't reproduced it in the shipping app, because that would mean editing the app's settings store by hand.

Steps To Reproduce

  1. Call UnitConverter::RestoreUserPreferences(L"|||"), for example from a test in CalculatorUnitTests/UnitConverterTest.cpp. In the app, this is equivalent to the local setting UnitConverterPreferences holding |||.
  2. StringToUnit receives an empty string, StringToVector returns an empty vector, and tokenList[0] is read out of bounds.

Expected behavior
Malformed preferences are ignored, as RestoreUserPreferences' doc comment already says ("If it does not have expected number of parameters, we will ignore it"), and the converter opens with its defaults.

Screenshots
N/A

Device and Application Information

  • OS Build: 10.0.26100.0
  • Architecture: X64
  • Application Version: 11.2607.0.0
  • Region: en-GB
  • Dev Version Installed: False

Additional context
Proposed fix:

  • Make StringToUnit and StringToCategory return std::optional and reject the wrong field count.
  • Have RestoreUserPreferences apply the saved state only when the from unit, the to unit and the category all parse, so a partly valid string doesn't change the current category either.
  • Add unit tests covering the round trip and several malformed inputs.

I have this change prepared and will open a PR linked to this issue once it's triaged.

Requested Assignment
If possible, I would like to fix this.

Dominant language
C#
Stars
31.1k
Forks
5.8k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/calculator

All issues in microsoft/calculator

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.