Unit converter reads out of bounds when the saved UnitConverterPreferences setting is malformed
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
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
- Call
UnitConverter::RestoreUserPreferences(L"|||"), for example from a test inCalculatorUnitTests/UnitConverterTest.cpp. In the app, this is equivalent to the local settingUnitConverterPreferencesholding|||. StringToUnitreceives an empty string,StringToVectorreturns an empty vector, andtokenList[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
StringToUnitandStringToCategoryreturnstd::optionaland reject the wrong field count. - Have
RestoreUserPreferencesapply 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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/calculator
-
Area: World-Readiness
Difficulty 1/5 Under an hour Newbie friendliness 90/100
microsoft/calculator#2522 ·
-
Area: World-Readiness
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
microsoft/calculator#2515 · 3 comments · 1 reaction ·
-
Integer overflow in negation in ratpowi32()Possibly taken @amitdey1987 claimed this 55 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
microsoft/calculator#2500 · 5 comments ·
-
Warning C4706 triggered in Grapher.cppPossibly taken @michaelbprice claimed this 302 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 72/100
microsoft/calculator#2360 ·
-
“绘图”功能并未正常画图Open
Difficulty 3/5 1-2 days Newbie friendliness 48/100
microsoft/calculator#2530 · 1 reaction ·
All issues in microsoft/calculator
Similar issues
-
area: scaffold status: needs-tests
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
provider:flagd type:fix
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
open-feature/dotnet-sdk-contrib#789 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 72/100
NimblePros/Metronome#23 ·
-
ci(runners): le workflow Mirror state advisory rougit a tort quand jq manque sur le slot (exit 127), alors que l'organe conclut "etat non lisible"Possibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
jsboige/CoursIA#28504 · 3 comments ·
Maintainers usually reply within 1 day
-
CustomUnits: Battle Armor squads play tank movement sounds (move-audio default ignores FakeVehicle)Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100