From be598757a1c03286c08fbec9972f586812a84b87 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Sat, 3 Apr 2010 22:53:06 +0300 Subject: [PATCH] Fix segmentation fault when calling with --errorlist and add a testcase for it. --- lib/checkother.cpp | 5 ++++- lib/cppcheck.cpp | 29 ++++++++++++++++------------- lib/cppcheck.h | 6 ++++++ test/testcppcheck.cpp | 8 ++++++++ 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 582e565b6..fdddf962d 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2629,7 +2629,10 @@ void CheckOther::zerodivError(const Token *tok) void CheckOther::mathfunctionCallError(const Token *tok) { - reportError(tok, Severity::error, "wrongmathcall", "Passing value " + tok->tokAt(2)->str() + " to " + tok->str() + "() leads to undefined result"); + if (tok) + reportError(tok, Severity::error, "wrongmathcall", "Passing value " + tok->tokAt(2)->str() + " to " + tok->str() + "() leads to undefined result"); + else + reportError(tok, Severity::error, "wrongmathcall", "Passing value " " to " "() leads to undefined result"); } void CheckOther::postIncrementError(const Token *tok, const std::string &var_name, const bool isIncrement) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index e9e8d5119..b96de2e8c 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -320,19 +320,7 @@ void CppCheck::parseFromArgs(int argc, const char* const argv[]) // print all possible error messages.. else if (strcmp(argv[i], "--errorlist") == 0) { - // call all "getErrorMessages" in all registered Check classes - std::cout << ErrorLogger::ErrorMessage::getXMLHeader(); - for (std::list::iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) - { - (*it)->getErrorMessages(); - } - - Tokenizer tokenizer(&_settings, 0); - tokenizer.getErrorMessages(); - - std::cout << ErrorLogger::ErrorMessage::getXMLFooter() << std::endl; - - return; + getErrorMessages(); } // documentation.. @@ -674,3 +662,18 @@ void CppCheck::reportStatus(unsigned int /*index*/, unsigned int /*max*/) { } + +void CppCheck::getErrorMessages() +{ + // call all "getErrorMessages" in all registered Check classes + std::cout << ErrorLogger::ErrorMessage::getXMLHeader(); + for (std::list::iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) + { + (*it)->getErrorMessages(); + } + + Tokenizer tokenizer(&_settings, 0); + tokenizer.getErrorMessages(); + + std::cout << ErrorLogger::ErrorMessage::getXMLFooter() << std::endl; +} diff --git a/lib/cppcheck.h b/lib/cppcheck.h index b0eca8e0e..ac0376738 100644 --- a/lib/cppcheck.h +++ b/lib/cppcheck.h @@ -124,6 +124,12 @@ public: _settings.terminate(); } + /** + * @brief Call all "getErrorMessages" in all registered Check classes. + * Also print out xml header and footer. + */ + void getErrorMessages(); + private: void checkFile(const std::string &code, const char FileName[]); diff --git a/test/testcppcheck.cpp b/test/testcppcheck.cpp index bf1f1fe51..2c9a5c9d0 100644 --- a/test/testcppcheck.cpp +++ b/test/testcppcheck.cpp @@ -54,6 +54,7 @@ private: TEST_CASE(include); TEST_CASE(templateFormat); + TEST_CASE(getErrorMessages); } void linenumbers() @@ -120,6 +121,13 @@ private: ASSERT_EQUALS("[some/{file}file.cpp:10]: (testSeverity) long testMessage", errmsg.toText()); ASSERT_EQUALS("testId-some/{file}file.cpp,testSeverity.10?{long testMessage}", errmsg.toText("{id}-{file},{severity}.{line}?{{message}}")); } + + void getErrorMessages() + { + errout.str(""); + CppCheck cppCheck(*this); + cppCheck.getErrorMessages(); + } }; REGISTER_TEST(TestCppcheck)