add support for deallocating memory with realloc
This commit is contained in:
parent
c1c4ffd700
commit
acebc635b5
|
@ -247,7 +247,8 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok
|
||||||
return NewArray;
|
return NewArray;
|
||||||
|
|
||||||
if (Token::Match(tok, "free|kfree ( %varid% ) ;", varid) ||
|
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;
|
return Malloc;
|
||||||
|
|
||||||
if (Token::Match(tok, "g_free ( %varid% ) ;", varid) ||
|
if (Token::Match(tok, "g_free ( %varid% ) ;", varid) ||
|
||||||
|
@ -285,7 +286,8 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok
|
||||||
return NewArray;
|
return NewArray;
|
||||||
|
|
||||||
if (Token::simpleMatch(tok, std::string("free ( " + varname + " ) ;").c_str()) ||
|
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;
|
return Malloc;
|
||||||
|
|
||||||
if (Token::simpleMatch(tok, std::string("g_free ( " + varname + " ) ;").c_str()))
|
if (Token::simpleMatch(tok, std::string("g_free ( " + varname + " ) ;").c_str()))
|
||||||
|
|
|
@ -277,6 +277,7 @@ private:
|
||||||
// * It is not ok to dereference a pointer to deallocated memory
|
// * It is not ok to dereference a pointer to deallocated memory
|
||||||
TEST_CASE(dealloc_use);
|
TEST_CASE(dealloc_use);
|
||||||
TEST_CASE(dealloc_use_2);
|
TEST_CASE(dealloc_use_2);
|
||||||
|
TEST_CASE(dealloc_use_3);
|
||||||
|
|
||||||
// free a free'd pointer
|
// free a free'd pointer
|
||||||
TEST_CASE(freefree1);
|
TEST_CASE(freefree1);
|
||||||
|
@ -2975,6 +2976,25 @@ private:
|
||||||
ASSERT_EQUALS("", errout.str());
|
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()
|
void freefree1()
|
||||||
{
|
{
|
||||||
check("void foo()\n"
|
check("void foo()\n"
|
||||||
|
|
Loading…
Reference in New Issue