Fixed #4231 (False positive: (error) Returning/dereferencing 'ptr' after it is deallocated / released (ignoring goto))

This commit is contained in:
Daniel Marjamäki 2012-10-08 17:23:47 +02:00
parent 61183e7896
commit f74c30e116
2 changed files with 19 additions and 0 deletions

View File

@ -467,6 +467,11 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
varInfo->clear(); varInfo->clear();
} }
// goto => weird execution path
else if (tok->str() == "goto") {
varInfo->clear();
}
// throw // throw
// TODO: if the execution leave the function then treat it as return // TODO: if the execution leave the function then treat it as return
else if (tok->str() == "throw") { else if (tok->str() == "throw") {

View File

@ -61,6 +61,7 @@ private:
// goto // goto
TEST_CASE(goto1); TEST_CASE(goto1);
TEST_CASE(goto2);
// if/else // if/else
TEST_CASE(ifelse1); TEST_CASE(ifelse1);
@ -324,6 +325,19 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void goto2() { // #4231
check("static char * f() {\n"
"x:\n"
" char *p = malloc(100);\n"
" if (err) {\n"
" free(p);\n"
" goto x;\n"
" }\n"
" return p;\n" // no error since there is a goto
"}");
ASSERT_EQUALS("", errout.str());
}
void ifelse1() { void ifelse1() {
check("int f() {\n" check("int f() {\n"
" char *p = NULL;\n" " char *p = NULL;\n"