Fixed #3895 (Improve check: double deallocation not detected (if-else))

This commit is contained in:
Daniel Marjamäki 2012-06-22 09:10:30 +02:00
parent 4987f6a4a9
commit 935351c601
3 changed files with 18 additions and 2 deletions

View File

@ -22,6 +22,8 @@
#include "checkleakautovar.h"
#include "checkother.h" // <- doubleFreeError
#include "tokenize.h"
#include "errorlogger.h"
#include "symboldatabase.h"
@ -423,7 +425,8 @@ void CheckLeakAutoVar::functionCall(const Token *tok, VarInfo *varInfo, const st
// possible usage
possibleUsage[arg->varId()] = tok->str();
} else if (var->second == "dealloc") {
// double deallocation is reported by CheckOther::checkDoubleFree
CheckOther checkOther(_tokenizer, _settings, _errorLogger);
checkOther.doubleFreeError(tok, arg->str());
} else if (var->second != dealloc) {
// mismatching allocation and deallocation
mismatchError(tok, arg->str());

View File

@ -234,6 +234,7 @@ public:
/** @brief %Check for double free or double close operations */
void checkDoubleFree();
void doubleFreeError(const Token *tok, const std::string &varname);
private:
// Error messages..
@ -285,7 +286,6 @@ private:
void bitwiseOnBooleanError(const Token *tok, const std::string &varname, const std::string &op);
void comparisonOfBoolExpressionWithIntError(const Token *tok, bool n0o1);
void SuspiciousSemicolonError(const Token *tok);
void doubleFreeError(const Token *tok, const std::string &varname);
void doubleCloseDirError(const Token *tok, const std::string &varname);
void moduloAlwaysTrueFalseError(const Token* tok, const std::string& maxVal);

View File

@ -49,6 +49,8 @@ private:
TEST_CASE(deallocuse3);
TEST_CASE(deallocuse4);
TEST_CASE(doublefree);
// exit
TEST_CASE(exit1);
TEST_CASE(exit2);
@ -240,6 +242,17 @@ private:
ASSERT_EQUALS("[test.c:3]: (error) Returning/using deallocated pointer p\n", errout.str());
}
void doublefree() { // #3895
check("void f(char *p) {\n"
" if (x)\n"
" free(p);\n"
" else\n"
" p = 0;\n"
" free(p);\n"
"}");
ASSERT_EQUALS("[test.c:6]: (error) Memory pointed to by 'p' is freed twice.\n", errout.str());
}
void exit1() {
check("void f() {\n"
" char *p = malloc(10);\n"