From 6d46346eb93d5032c08cf1e39023b5d57260a766 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Tue, 29 Aug 2017 12:21:29 -0400 Subject: Detect integer overflow/underflow --- libtests/qtest/qutil/qutil.out | 8 +++++ libtests/qutil.cc | 67 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) (limited to 'libtests') diff --git a/libtests/qtest/qutil/qutil.out b/libtests/qtest/qutil/qutil.out index 7a0bba48..c76beb2b 100644 --- a/libtests/qtest/qutil/qutil.out +++ b/libtests/qtest/qutil/qutil.out @@ -14,6 +14,14 @@ one 7 compare okay +-2147483648 to int: PASSED +2147483647 to int: PASSED +2147483648 to int threw: PASSED +-2147483649 to int threw: PASSED +9999999999999999999999999 to int threw: PASSED +2147483648 to int: PASSED +-2147483649 to int: PASSED +99999999999999999999999999999999999999999999999999 to int threw: PASSED ---- before remove exception: remove file: No such file or directory diff --git a/libtests/qutil.cc b/libtests/qutil.cc index 04ca03c0..7fc5960d 100644 --- a/libtests/qutil.cc +++ b/libtests/qutil.cc @@ -6,6 +6,7 @@ #include #include #include +#include #ifdef _WIN32 # include @@ -13,6 +14,57 @@ # include #endif +template +void test_to_number(char const* str, int_T wanted, bool error, + int_T (*fn)(char const*)) +{ + bool threw = false; + bool worked = false; + int_T result = 0; + try + { + result = fn(str); + worked = (wanted == result); + } + catch (std::runtime_error) + { + threw = true; + } + if (threw) + { + if (error) + { + std::cout << str << " to int threw: PASSED" << std::endl; + } + else + { + std::cout << str << " to int threw but wanted " + << wanted << std::endl; + } + } + else + { + if (worked) + { + std::cout << str << " to int: PASSED" << std::endl; + } + else + { + std::cout << str << " to int failed; got " << result << std::endl; + } + } +} + +void test_to_int(char const* str, int wanted, bool error) +{ + test_to_number(str, wanted, error, QUtil::string_to_int); +} + +void test_to_ll(char const* str, long long wanted, bool error) +{ + test_to_number(str, wanted, error, QUtil::string_to_ll); +} + void string_conversion_test() { std::cout << QUtil::int_to_string(16059) << std::endl @@ -44,6 +96,21 @@ void string_conversion_test() std::cout << "compare failed" << std::endl; } delete [] tmp; + + std::string int_max_str = QUtil::int_to_string(INT_MAX); + std::string int_min_str = QUtil::int_to_string(INT_MIN); + long long int_max_plus_1 = static_cast(INT_MAX) + 1; + long long int_min_minus_1 = static_cast(INT_MIN) - 1; + std::string int_max_plus_1_str = QUtil::int_to_string(int_max_plus_1); + std::string int_min_minus_1_str = QUtil::int_to_string(int_min_minus_1); + test_to_int(int_min_str.c_str(), INT_MIN, false); + test_to_int(int_max_str.c_str(), INT_MAX, false); + test_to_int(int_max_plus_1_str.c_str(), 0, true); + test_to_int(int_min_minus_1_str.c_str(), 0, true); + test_to_int("9999999999999999999999999", 0, true); + test_to_ll(int_max_plus_1_str.c_str(), int_max_plus_1, false); + test_to_ll(int_min_minus_1_str.c_str(), int_min_minus_1, false); + test_to_ll("99999999999999999999999999999999999999999999999999", 0, true); } void os_wrapper_test() -- cgit v1.2.3-54-g00ecf