CheckLeakAutoVar: Updated error messages

This commit is contained in:
Daniel Marjamäki 2012-06-30 16:23:10 +02:00
parent bb9f114d84
commit 325414e354
3 changed files with 29 additions and 18 deletions

View File

@ -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<const Token *> 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)

View File

@ -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.";
}
};
/// @}

View File

@ -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() {