Reject partial parses in stou32 - #4555
Open
Daksha1611 wants to merge 1 commit into
Open
Conversation
stou32() is the only converter in stringutils that does not verify the whole
string was consumed. std::stoul stops at the first character it cannot use
and still reports success, so:
stou32("12abc") == 12
stou32("3.9") == 3
stou32("0x10") == 0
stou32("100%") == 100
Its siblings all guard against this by passing &idx and comparing against
the length - stou64 at :143-147, stoi32 at :161-165, stof at :203-206.
(stoi64 at :181-185 validates by scanning digits first, which is
equivalent.) Add the same check.
Call sites this affects:
grpcservermodule.cpp:79 - the GRPC_SERVERS environment variable.
GRPC_SERVERS=4x silently started 4 servers instead of being rejected
and falling back to config.grpcWorkers().
s2t_servable.cpp:81 - the transcription temperature field, where the
value is used as a fallback after ovms::stof has already rejected it.
The surrounding erase_spaces() call is deliberately left alone, so
stou32("12 34") still yields 1234. That is a separate question from partial
parsing - note stou64 rejects " 100 " outright - and changing it would be
a second behaviour change in one commit.
Tests: the existing StringUtils.stou32 case only covers a negative value,
overflow and the maximum, none of which change. Adds
StringUtils.stou32RejectsPartialParse for the trailing-garbage spellings
above, plus the plain-number cases to pin that valid input still parses.
Daksha1611
marked this pull request as ready for review
September 12, 2026 07:09
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🛠 Summary
Fixes #4554.
stou32()is the only converter instringutilsthat does not verify the whole string was consumed.std::stoulstops at the first character it cannot use and still reports success, sostou32("12abc")returned 12,stou32("3.9")returned 3 andstou32("0x10")returned 0. Its siblings all guard against this by passing&idxand comparing against the length —stou64at:143-147,stoi32at:161-165,stofat:203-206. This adds the same check.Two call sites are affected.
grpcservermodule.cpp:79reads theGRPC_SERVERSenvironment variable, soGRPC_SERVERS=4xsilently started 4 servers instead of being rejected and falling back toconfig.grpcWorkers().s2t_servable.cpp:81uses it as a fallback for the transcription temperature field; that endpoint is also addressed in #4553, and either change alone fixes it — they do not conflict.The surrounding
erase_spaces()call is deliberately left alone, sostou32("12 34")still yields 1234. That is a separate question from partial parsing — notestou64rejects" 100 "outright — and I did not want two behaviour changes in one commit.The existing
StringUtils.stou32case covers a negative value, overflow and the maximum, none of which change. AddsStringUtils.stou32RejectsPartialParsefor the trailing-garbage spellings plus plain numbers.I have no OVMS build container available, so this is not compiled against the full tree and CI will need to confirm the build. I did check the change in isolation and confirmed the existing test inputs are unaffected. Draft for that reason.
🧪 Checklist