add support for deallocating memory with realloc

This commit is contained in:
Robert Reif 2011-08-30 23:42:11 -04:00
parent c1c4ffd700
commit acebc635b5
2 changed files with 24 additions and 2 deletions

View File

@ -247,7 +247,8 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok
return NewArray;
if (Token::Match(tok, "free|kfree ( %varid% ) ;", varid) ||
Token::Match(tok, "free|kfree ( %varid% -", varid))
Token::Match(tok, "free|kfree ( %varid% -", varid) ||
Token::Match(tok, "realloc ( %varid% , 0 ) ;", varid))
return Malloc;
if (Token::Match(tok, "g_free ( %varid% ) ;", varid) ||
@ -285,7 +286,8 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok
return NewArray;
if (Token::simpleMatch(tok, std::string("free ( " + varname + " ) ;").c_str()) ||
Token::simpleMatch(tok, std::string("kfree ( " + varname + " ) ;").c_str()))
Token::simpleMatch(tok, std::string("kfree ( " + varname + " ) ;").c_str()) ||
Token::simpleMatch(tok, std::string("realloc ( " + varname + " , 0 ) ;").c_str()))
return Malloc;
if (Token::simpleMatch(tok, std::string("g_free ( " + varname + " ) ;").c_str()))

View File

@ -277,6 +277,7 @@ private:
// * It is not ok to dereference a pointer to deallocated memory
TEST_CASE(dealloc_use);
TEST_CASE(dealloc_use_2);
TEST_CASE(dealloc_use_3);
// free a free'd pointer
TEST_CASE(freefree1);
@ -2975,6 +2976,25 @@ private:
ASSERT_EQUALS("", errout.str());
}
void dealloc_use_3()
{
check("void foo()\n"
"{\n"
" char *str = malloc(10);\n"
" realloc(str, 0);\n"
" str[10] = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Dereferencing 'str' after it is deallocated / released\n", errout.str());
check("void foo()\n"
"{\n"
" char *str = realloc(0, 10);\n"
" realloc(str, 0);\n"
" str[10] = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Dereferencing 'str' after it is deallocated / released\n", errout.str());
}
void freefree1()
{
check("void foo()\n"