Fixed #2298 (new check: passing stack-address to free())

This commit is contained in:
Alexander Mai 2014-03-06 06:32:30 +01:00 committed by Daniel Marjamäki
parent ca0509e20b
commit 354e84e7c8
2 changed files with 21 additions and 1 deletions

View File

@ -201,6 +201,11 @@ void CheckAutoVariables::autoVariables()
if (isAutoVarArray(tok))
errorInvalidDeallocation(tok);
}
else if (Token::Match(tok, "free ( & %var% ) ;") || Token::Match(tok, "delete [| ]| (| & %var% !![")) {
tok = Token::findmatch(tok->next(), "%var%");
if (tok->variable()->isLocal())
errorInvalidDeallocation(tok);
}
}
}
}

View File

@ -472,7 +472,22 @@ private:
" free(psz_title);\n"
" }\n"
"}");
ASSERT_EQUALS(std::string(""), errout.str());
ASSERT_EQUALS("", errout.str());
// #2298 new check: passing stack-address to free()
check("int main() {\n"
" int *p = malloc(4);\n"
" free(&p);\n"
" return 0;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Deallocation of an auto-variable results in undefined behaviour.\n", errout.str());
check("int main() {\n"
" int i;\n"
" free(&i);\n"
" return 0;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Deallocation of an auto-variable results in undefined behaviour.\n", errout.str());
}
void testassign1() { // Ticket #1819