From b6bcdf29363a25112b8884fc9944237f1461402d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 9 May 2013 18:50:24 +0200 Subject: [PATCH] Fixed #4520 (segmentation fault of cppcheck (preprocessing)) --- lib/cppcheck.cpp | 29 ++++++++++++++++++++++++++--- lib/cppcheck.h | 3 +++ lib/mathlib.cpp | 4 ++++ test/testmathlib.cpp | 1 + 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 2202cb053..84366838e 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -210,9 +210,9 @@ unsigned int CppCheck::processFile(const std::string& filename) } } } catch (const std::runtime_error &e) { - // Exception was thrown when checking this file.. - const std::string fixedpath = Path::toNativeSeparators(filename); - _errorLogger.reportOut("Bailing out from checking " + fixedpath + ": " + e.what()); + internalError(filename, e.what()); + } catch (const InternalError &e) { + internalError(filename, e.errorMessage); } if (!_settings._errorsOnly) @@ -222,6 +222,29 @@ unsigned int CppCheck::processFile(const std::string& filename) return exitcode; } +void CppCheck::internalError(const std::string &filename, const std::string &msg) +{ + const std::string fixedpath = Path::toNativeSeparators(filename); + const std::string fullmsg("Bailing out from checking " + fixedpath + " since there was a internal error: " + msg); + + if (_settings.isEnabled("information")) { + const ErrorLogger::ErrorMessage::FileLocation loc1(filename, 0); + std::list callstack; + callstack.push_back(loc1); + + ErrorLogger::ErrorMessage errmsg(callstack, + Severity::information, + fullmsg, + "internalError", + false); + + _errorLogger.reportErr(errmsg); + + } else { + // Report on stdout + _errorLogger.reportOut(fullmsg); + } +} void CppCheck::checkFunctionUsage() diff --git a/lib/cppcheck.h b/lib/cppcheck.h index e698933cb..5ef01df1e 100644 --- a/lib/cppcheck.h +++ b/lib/cppcheck.h @@ -135,6 +135,9 @@ public: private: + /** @brief There has been a internal error => Report information message */ + void internalError(const std::string &filename, const std::string &msg); + /** @brief Process one file. */ unsigned int processFile(const std::string& filename); diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index b8ab52808..87dc221c4 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -26,6 +26,7 @@ #include #include #include +#include MathLib::bigint MathLib::toLongNumber(const std::string &str) { @@ -280,7 +281,10 @@ std::string MathLib::subtract(const std::string &first, const std::string &secon std::string MathLib::divide(const std::string &first, const std::string &second) { if (MathLib::isInt(first) && MathLib::isInt(second)) { + bigint a = toLongNumber(first); bigint b = toLongNumber(second); + if (a == std::numeric_limits::min()) + throw InternalError(0, "Internal Error: Division overflow"); if (b == 0) throw InternalError(0, "Internal Error: Division by zero"); return longToString(toLongNumber(first) / b); diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index 8101396b8..037471549 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -124,6 +124,7 @@ private: ASSERT_EQUALS("7.0" , MathLib::divide("21.", "3")); ASSERT_EQUALS("1" , MathLib::divide("3", "2")); ASSERT_THROW(MathLib::divide("123", "0"), InternalError); // throw + ASSERT_THROW(MathLib::divide("-9223372036854775808", "-1"), InternalError); // #4520 - out of range => throw MathLib::divide("123", "0.0"); // don't throw // Unknown action should throw exception