Fixed #1439 (false positive: memory leak when using realloc)
This commit is contained in:
parent
c9ff53fbed
commit
50210857a6
|
@ -2945,6 +2945,36 @@ bool Tokenizer::simplifyTokenList()
|
|||
simplifyLogicalOperators();
|
||||
simplifyCasts();
|
||||
|
||||
// simplify "x=realloc(y,0);" => "free(y); x=0;"..
|
||||
for (Token *tok = _tokens; tok; tok = tok->next())
|
||||
{
|
||||
if (Token::Match(tok, "; %var% = realloc ( %var% , 0 ) ;"))
|
||||
{
|
||||
const std::string varname(tok->next()->str());
|
||||
const unsigned int varid(tok->next()->varId());
|
||||
|
||||
// Delete the "%var% ="
|
||||
tok->deleteNext();
|
||||
tok->deleteNext();
|
||||
|
||||
// Change function name "realloc" to "free"
|
||||
tok->next()->str("free");
|
||||
|
||||
// delete the ", 0"
|
||||
Token::eraseTokens(tok->tokAt(3), tok->tokAt(6));
|
||||
|
||||
// goto the ";"
|
||||
tok = tok->tokAt(5);
|
||||
|
||||
// insert "var=0;"
|
||||
tok->insertToken(";");
|
||||
tok->insertToken("0");
|
||||
tok->insertToken("=");
|
||||
tok->insertToken(varname);
|
||||
tok->next()->varId(varid);
|
||||
}
|
||||
}
|
||||
|
||||
// Simplify simple calculations..
|
||||
while (simplifyCalculations())
|
||||
;
|
||||
|
|
|
@ -211,6 +211,9 @@ private:
|
|||
|
||||
// Tokenizer::simplifyReference
|
||||
TEST_CASE(simplifyReference);
|
||||
|
||||
// x = realloc(y,0); => free(y);x=0;
|
||||
TEST_CASE(simplifyRealloc);
|
||||
}
|
||||
|
||||
std::string tok(const char code[], bool simplify = true)
|
||||
|
@ -3993,6 +3996,12 @@ private:
|
|||
ASSERT_EQUALS("void f ( ) { int a ; a ++ ; }",
|
||||
tok("void f() { int a; int &b = a; b++; }"));
|
||||
}
|
||||
|
||||
void simplifyRealloc()
|
||||
{
|
||||
ASSERT_EQUALS("; free ( p ) ; p = 0 ;",
|
||||
tok("; p = realloc(p,0);"));
|
||||
}
|
||||
};
|
||||
|
||||
REGISTER_TEST(TestSimplifyTokens)
|
||||
|
|
Loading…
Reference in New Issue