Fixed ticket #3550 (false positive: (error) Memory pointed to by 'pxpm' is freed twice)

This commit is contained in:
Zachary Blair 2012-01-24 22:43:44 -08:00
parent b6afa8a025
commit 589a2461bd
3 changed files with 30 additions and 7 deletions

View File

@ -2514,14 +2514,24 @@ void CheckOther::checkDoubleFree()
closeDirVariables.clear();
}
// If a variable is passed to a function, remove it from the set of previously freed variables
else if (Token::Match(tok, "%var% (") && !Token::Match(tok, "printf|sprintf|snprintf|fprintf")) {
for (const Token* tok2 = tok->tokAt(2); tok2 != tok->linkAt(1); tok2 = tok2->next()) {
if (Token::Match(tok2, "%var%")) {
unsigned int var = tok2->varId();
if (var) {
freedVariables.erase(var);
closeDirVariables.erase(var);
else if (Token::Match(tok, "%var% (") && !Token::Match(tok, "printf|sprintf|snprintf|fprintf|if|while")) {
// If this is a new function definition, clear all variables
if (Token::Match(tok->next()->link(), ") {")) {
freedVariables.clear();
closeDirVariables.clear();
}
// If it is a function call, then clear those variables in its argument list
else if (Token::Match(tok->next()->link(), ") ;")) {
for (const Token* tok2 = tok->tokAt(2); tok2 != tok->linkAt(1); tok2 = tok2->next()) {
if (Token::Match(tok2, "%var%")) {
unsigned int var = tok2->varId();
if (var) {
freedVariables.erase(var);
closeDirVariables.erase(var);
}
}
}
}

View File

@ -329,6 +329,7 @@ public:
c.sizeofForStrncmpError(0);
c.sizeofForNumericParameterError(0);
c.coutCerrMisusageError(0, "cout");
c.doubleFreeError(0, "varname");
// style/warning
c.cstyleCastError(0);

View File

@ -4679,6 +4679,18 @@ private:
" delete[] p;\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory pointed to by 'p' is freed twice.\n", errout.str());
check(
"~LineMarker() {"
" delete pxpm;"
"}"
"LineMarker &operator=(const LineMarker &) {"
" delete pxpm;"
" pxpm = NULL;"
" return *this;"
"}"
);
ASSERT_EQUALS("", errout.str());
}
void coutCerrMisusage() {