From d9a594e1845060cc3143f22f45f10a86e5b52094 Mon Sep 17 00:00:00 2001 From: Pascal Bach Date: Mon, 28 Apr 2014 16:14:17 +0200 Subject: [PATCH] NumberParser::parseUnsigned should not parse negative numbers The function should abort if a negative number (e.g. "-123") is passed as input --- Foundation/include/Poco/NumericString.h | 2 ++ Foundation/testsuite/src/NumberParserTest.cpp | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/Foundation/include/Poco/NumericString.h b/Foundation/include/Poco/NumericString.h index 8dc18abdb..4bb7dca35 100644 --- a/Foundation/include/Poco/NumericString.h +++ b/Foundation/include/Poco/NumericString.h @@ -110,6 +110,8 @@ bool strToInt(const char* pStr, I& result, short base, char thSep = ',') char sign = 1; if ((base == 10) && (*pStr == '-')) { + // Unsigned types can't be negative so abort parsing + if (std::numeric_limits::min() >= 0) return false; sign = -1; ++pStr; } diff --git a/Foundation/testsuite/src/NumberParserTest.cpp b/Foundation/testsuite/src/NumberParserTest.cpp index 967236263..7ee05e693 100644 --- a/Foundation/testsuite/src/NumberParserTest.cpp +++ b/Foundation/testsuite/src/NumberParserTest.cpp @@ -272,6 +272,12 @@ void NumberParserTest::testParseError() failmsg("must throw SyntaxException"); } catch (SyntaxException&) { } + try + { + NumberParser::parseUnsigned("-123"); + failmsg("must throw SyntaxException"); + } catch (SyntaxException&) { } + try { NumberParser::parseHex("z23");