Simplify "x = realloc (0, n);" to "x = malloc(n);"
This commit is contained in:
parent
e75a81669e
commit
3d9550b0bf
|
@ -4116,6 +4116,7 @@ bool Tokenizer::simplifyTokenList()
|
|||
}
|
||||
|
||||
// simplify "x=realloc(y,0);" => "free(y); x=0;"..
|
||||
// and "x = realloc (0, n);" => "x = malloc(n);"
|
||||
for (Token *tok = _tokens; tok; tok = tok->next())
|
||||
{
|
||||
if (Token::Match(tok, "; %var% = realloc ( %var% , 0 ) ;"))
|
||||
|
@ -4143,6 +4144,18 @@ bool Tokenizer::simplifyTokenList()
|
|||
tok->insertToken(varname);
|
||||
tok->next()->varId(varid);
|
||||
}
|
||||
else if (Token::Match(tok, "; %var% = realloc ( 0 , %num% ) ;"))
|
||||
{
|
||||
const std::string varname(tok->next()->str());
|
||||
|
||||
tok = tok->tokAt(3);
|
||||
// Change function name "realloc" to "malloc"
|
||||
tok->str("malloc");
|
||||
|
||||
// delete "0 ,"
|
||||
tok->next()->deleteNext();
|
||||
tok->next()->deleteNext();
|
||||
}
|
||||
}
|
||||
|
||||
// Change initialisation of variable to assignment
|
||||
|
|
|
@ -6004,6 +6004,10 @@ private:
|
|||
{
|
||||
ASSERT_EQUALS("; free ( p ) ; p = 0 ;",
|
||||
tok("; p = realloc(p,0);"));
|
||||
ASSERT_EQUALS("; p = malloc ( 100 ) ;",
|
||||
tok("; p = realloc(0, 100);"));
|
||||
ASSERT_EQUALS("; p = malloc ( 0 ) ;",
|
||||
tok("; p = realloc(0, sizeof(char)*0);"));
|
||||
}
|
||||
|
||||
void simplifyErrNoInWhile()
|
||||
|
|
Loading…
Reference in New Issue