Fixed Ticket #3876 (Error (double free) detected that can't possibly happen)

This commit is contained in:
Zachary Blair 2012-06-10 17:38:31 -07:00
parent 5b763a9f0a
commit e2348560e4
2 changed files with 52 additions and 0 deletions

View File

@ -2292,6 +2292,13 @@ void CheckOther::checkDoubleFree()
closeDirVariables.clear(); closeDirVariables.clear();
} }
// If this scope is a "for" or "while" loop, give up on trying to figure
// out the flow of execution and just clear the set of previously freed variables
else if (tok->str() == "}" && tok->link() && tok->link()->previous() && tok->link()->previous()->link() &&
Token::Match(tok->link()->previous()->link()->previous(), "while|for")) {
freedVariables.clear();
closeDirVariables.clear();
}
// If a variable is passed to a function, remove it from the set of previously freed variables // 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")) { else if (Token::Match(tok, "%var% (") && !Token::Match(tok, "printf|sprintf|snprintf|fprintf")) {

View File

@ -5087,6 +5087,51 @@ private:
"}" "}"
); );
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check(
"void foo(int y)\n"
"{\n"
" char * x = NULL;\n"
" while(1) {\n"
" x = new char[100];\n"
" if (y++ > 100)\n"
" break;\n"
" delete[] x;\n"
" }\n"
" delete[] x;\n"
"}"
);
ASSERT_EQUALS("", errout.str());
check(
"void foo(int y)\n"
"{\n"
" char * x = NULL;\n"
" for (;;) {\n"
" x = new char[100];\n"
" if (y++ > 100)\n"
" break;\n"
" delete[] x;\n"
" }\n"
" delete[] x;\n"
"}"
);
ASSERT_EQUALS("", errout.str());
check(
"void foo(int y)\n"
"{\n"
" char * x = NULL;\n"
" do {\n"
" x = new char[100];\n"
" if (y++ > 100)\n"
" break;\n"
" delete[] x;\n"
" } while (1);\n"
" delete[] x;\n"
"}"
);
ASSERT_EQUALS("", errout.str());
} }
}; };