Skip to content

fix(Foundation): strToInt rejects a minus sign after a plus sign - #5598

Open
555aaditya wants to merge 1 commit into
pocoproject:mainfrom
555aaditya:numberparser-reject-sign-after-plus
Open

555aaditya wants to merge 1 commit into
pocoproject:mainfrom
555aaditya:numberparser-reject-sign-after-plus

Conversation

@555aaditya

Copy link
Copy Markdown
Contributor

NumberParser::parse("+-5") returns -5. Since #5301, strToInt() skips a leading + and passes the rest to std::from_chars, which accepts a - for signed types. So +-5 (and, for signed types, +-ff in base 16) is accepted as a negative number. The digit loop before #5301 rejected it.

  • Foundation/include/Poco/NumericString.h: strToInt() returns false when a - follows the skipped +. A single leading + or - is handled as before.

Tests: StringTest::stringToInt<T>() (all integer types, base 10 and 16) and NumberParserTest::testParseError assert that +-123 is 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):

  • the full Foundation suite with -ignore cppignore.lnx (1004 tests, OK);
  • StringTest and NumberParserTest under ASan, and StringTest::testStringToInt and NumberParserTest under UBSan;
  • clang-tidy and cppcheck, no findings on the changed lines.

Under UBSan, StringTest::conversionBenchmarks aborts on a signed overflow of gSink in StringTest.cpp:1492. It does the same on main without this change.

Follow-up: the comment in XSD/CodeGen/src/BuiltinTypes.cpp gives "+-5" as an example. The check there is still needed for leading whitespace.

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 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

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.

2 participants