Repository navigation
fix(Foundation): strToInt rejects a minus sign after a plus sign - #5598
Open
555aaditya wants to merge 1 commit into
Open
555aaditya wants to merge 1 commit into
555aaditya wants to merge 1 commit into
Conversation
Since pocoproject#5301, strToInt skips a leading '+' and hands the rest to std::from_chars, which accepts '-' for signed types, so "+-5" parsed as -5.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The narrowly scoped guard safely rejects malformed signs and includes regression coverage at both conversion and public API levels.
No open issues 🎉
What changed in this PR
Fixes an integer-parsing regression introduced by #5301 by rejecting a minus sign immediately after a leading plus sign, while preserving valid single-sign inputs.
Changes:
- Adds a targeted guard in
strToInt()for malformed+-prefixes. - Adds regression tests for decimal and hexadecimal conversion and the public
NumberParser::parse()API.
| File | Description |
|---|---|
Foundation/testsuite/src/StringTest.h |
Tests rejection of +-123 in bases 10 and 16 across integer types. |
Foundation/testsuite/src/NumberParserTest.cpp |
Verifies that NumberParser::parse("+-123") throws SyntaxException. |
Foundation/include/Poco/NumericString.h |
Rejects a minus sign after a skipped leading plus sign. |
Review effort: Balanced
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.
NumberParser::parse("+-5")returns -5. Since #5301,strToInt()skips a leading+and passes the rest tostd::from_chars, which accepts a-for signed types. So+-5(and, for signed types,+-ffin base 16) is accepted as a negative number. The digit loop before #5301 rejected it.Foundation/include/Poco/NumericString.h:strToInt()returnsfalsewhen a-follows the skipped+. A single leading+or-is handled as before.Tests:
StringTest::stringToInt<T>()(all integer types, base 10 and 16) andNumberParserTest::testParseErrorassert that+-123is rejected. Both fail without the change and pass with it.Validation on macOS arm64, Apple clang 21, Debug,
ENABLE_COMPILER_WARNINGS=ON(no warnings in the changed file):-ignore cppignore.lnx(1004 tests, OK);StringTestandNumberParserTestunder ASan, andStringTest::testStringToIntandNumberParserTestunder UBSan;Under UBSan,
StringTest::conversionBenchmarksaborts on a signed overflow ofgSinkinStringTest.cpp:1492. It does the same onmainwithout this change.Follow-up: the comment in
XSD/CodeGen/src/BuiltinTypes.cppgives"+-5"as an example. The check there is still needed for leading whitespace.