From 325414e3546861e7a9bce7c407a7dc9e7d520e5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 30 Jun 2012 16:23:10 +0200 Subject: [PATCH] CheckLeakAutoVar: Updated error messages --- lib/checkleakautovar.cpp | 20 ++++++++++++++++---- lib/checkleakautovar.h | 9 ++++----- test/testleakautovar.cpp | 18 +++++++++--------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index e4d246d38..16a4a89b9 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -22,6 +22,7 @@ #include "checkleakautovar.h" +#include "checkmemoryleak.h" // <- CheckMemoryLeak::memoryLeak #include "checkother.h" // <- doubleFreeError #include "tokenize.h" @@ -65,22 +66,33 @@ void VarInfo::possibleUsageAll(const std::string &functionName) void CheckLeakAutoVar::leakError(const Token *tok, const std::string &varname) { - reportError(tok, Severity::error, "newleak", "New memory leak: " + varname); + const Standards standards; + CheckMemoryLeak checkmemleak(_tokenizer, _errorLogger, standards); + checkmemleak.memleakError(tok, varname); + //reportError(tok, Severity::error, "newleak", "New memory leak: " + varname); } void CheckLeakAutoVar::mismatchError(const Token *tok, const std::string &varname) { - reportError(tok, Severity::error, "newmismatch", "New mismatching allocation and deallocation: " + varname); + const Standards standards; + CheckMemoryLeak c(_tokenizer, _errorLogger, standards); + std::list callstack; + callstack.push_back(tok); + c.mismatchAllocDealloc(callstack, varname); + //reportError(tok, Severity::error, "newmismatch", "New mismatching allocation and deallocation: " + varname); } void CheckLeakAutoVar::deallocUseError(const Token *tok, const std::string &varname) { - reportError(tok, Severity::error, "newdeallocuse", "Using deallocated pointer " + varname); + const Standards standards; + CheckMemoryLeak c(_tokenizer, _errorLogger, standards); + c.deallocuseError(tok, varname); + //reportError(tok, Severity::error, "newdeallocuse", "Using deallocated pointer " + varname); } void CheckLeakAutoVar::deallocReturnError(const Token *tok, const std::string &varname) { - reportError(tok, Severity::error, "newdeallocret", "Returning/using deallocated pointer " + varname); + reportError(tok, Severity::error, "deallocret", "Returning/dereferencing '" + varname + "' after it is deallocated / released"); } void CheckLeakAutoVar::configurationInfo(const Token* tok, const std::string &functionName) diff --git a/lib/checkleakautovar.h b/lib/checkleakautovar.h index ac6593d68..db2160327 100644 --- a/lib/checkleakautovar.h +++ b/lib/checkleakautovar.h @@ -119,18 +119,17 @@ private: void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const { CheckLeakAutoVar c(0, settings, errorLogger); - c.leakError(NULL, "p"); - c.mismatchError(NULL, "p"); - c.deallocUseError(NULL, "p"); + c.deallocReturnError(0, "p"); c.configurationInfo(0, "f"); // user configuration is needed to complete analysis } std::string myName() const { - return "Leaks in functions"; + return "CheckLeakAutoVar"; } std::string classInfo() const { - return ""; + return "Checking that detect leaks when a local variable is allocated but not deallocated. " + "This checking complements the other checking for memory leaks that Cppcheck has."; } }; /// @} diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index e770ddaec..3b6af9306 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -115,7 +115,7 @@ private: " p = NULL;\n" " free(p);\n" "}\n"); - ASSERT_EQUALS("[test.c:3]: (error) New memory leak: p\n", errout.str()); + ASSERT_EQUALS("[test.c:3]: (error) Memory leak: p\n", errout.str()); } void assign2() { @@ -203,13 +203,13 @@ private: " free(p);\n" " *p = 0;\n" "}"); - ASSERT_EQUALS("[test.c:3]: (error) Using deallocated pointer p\n", errout.str()); + ASSERT_EQUALS("[test.c:3]: (error) Dereferencing 'p' after it is deallocated / released\n", errout.str()); check("void f(char *p) {\n" " free(p);\n" " char c = *p;\n" "}"); - ASSERT_EQUALS("[test.c:3]: (error) Using deallocated pointer p\n", errout.str()); + ASSERT_EQUALS("[test.c:3]: (error) Dereferencing 'p' after it is deallocated / released\n", errout.str()); } void deallocuse2() { @@ -231,7 +231,7 @@ private: " free(p);\n" " p = p->next;\n" "}"); - ASSERT_EQUALS("[test.c:3]: (error) Using deallocated pointer p\n", errout.str()); + ASSERT_EQUALS("[test.c:3]: (error) Dereferencing 'p' after it is deallocated / released\n", errout.str()); } void deallocuse4() { @@ -239,7 +239,7 @@ private: " free(p);\n" " return p;\n" "}"); - ASSERT_EQUALS("[test.c:3]: (error) Returning/using deallocated pointer p\n", errout.str()); + ASSERT_EQUALS("[test.c:3]: (error) Returning/dereferencing 'p' after it is deallocated / released\n", errout.str()); } void doublefree() { // #3895 @@ -296,7 +296,7 @@ private: " if (x) { p = malloc(10); }\n" " else { return 0; }\n" "}"); - ASSERT_EQUALS("[test.c:5]: (error) New memory leak: p\n", errout.str()); + ASSERT_EQUALS("[test.c:5]: (error) Memory leak: p\n", errout.str()); } void ifelse3() { @@ -357,7 +357,7 @@ private: " else\n" " a = 0;\n" "}\n"); - ASSERT_EQUALS("[test.c:6]: (error) New memory leak: a\n", errout.str()); + ASSERT_EQUALS("[test.c:6]: (error) Memory leak: a\n", errout.str()); } void switch1() { @@ -388,7 +388,7 @@ private: " FILE*f=fopen(fname,a);\n" " free(f);\n" "}"); - ASSERT_EQUALS("[test.c:3]: (error) New mismatching allocation and deallocation: f\n", errout.str()); + ASSERT_EQUALS("[test.c:3]: (error) Mismatching allocation and deallocation: f\n", errout.str()); } void return1() { @@ -396,7 +396,7 @@ private: " char *p = malloc(100);\n" " return 123;\n" "}"); - ASSERT_EQUALS("[test.c:3]: (error) New memory leak: p\n", errout.str()); + ASSERT_EQUALS("[test.c:3]: (error) Memory leak: p\n", errout.str()); } void return2() {