fix(strToInt): overflows #3580

This commit is contained in:
Alex Fabijanic
2022-06-22 23:13:39 +02:00
parent 6a97657df8
commit 284a141432
3 changed files with 281 additions and 208 deletions

View File

@@ -19,6 +19,7 @@
#include "Poco/NumericString.h"
#include "Poco/MemoryStream.h"
#include "Poco/NumberFormatter.h"
#include <limits>
class StringTest: public CppUnit::TestCase
@@ -53,12 +54,14 @@ public:
void testNumericStringLimit();
void testStringToFloatError();
void testNumericLocale();
void benchmarkStrToFloat();
void benchmarkStrToInt();
void testIntToString();
void testFloatToString();
void conversionBenchmarks();
void benchmarkFloatToStr();
void benchmarkStrToFloat();
void benchmarkStrToInt();
void testJSONString();
@@ -128,7 +131,6 @@ private:
assertFalse(s == std::numeric_limits<Smaller>::min());
assertTrue(Poco::strToInt<Smaller>(val, s, 10));
assertTrue (s == std::numeric_limits<Smaller>::min());
assertTrue(s == std::numeric_limits<Smaller>::min());
--l; val = Poco::NumberFormatter::format(l);
assertFalse(Poco::strToInt<Smaller>(val, s, 10));
--l; val = Poco::NumberFormatter::format(l);
@@ -143,6 +145,21 @@ private:
assertFalse(Poco::strToInt<Smaller>(val, s, 10));
}
template <typename T>
void multiplyOverflow()
{
T m = static_cast<T>(10);
T t = 0;
T f = std::numeric_limits<T>::max()/m;
assertTrue (Poco::safeMultiply(t, f, m));
f += 1;
assertFalse (Poco::safeMultiply(t, f, m));
f = std::numeric_limits<T>::min()/m;
assertTrue (Poco::safeMultiply(t, f, m));
f -= 1;
assertFalse (Poco::safeMultiply(t, f, m));
}
template <typename T>
bool parseStream(const std::string& s, T& value)
{