Fixed #2784 (Pointer issue: *&f=open())

This commit is contained in:
WenChung Chiu 2011-05-18 07:25:30 +02:00 committed by Daniel Marjamäki
parent b6dcdd7b79
commit 1ea52cfa02
3 changed files with 48 additions and 0 deletions

View File

@ -2019,6 +2019,28 @@ void Tokenizer::simplifyTypedef()
}
}
void Tokenizer::simplifyMulAnd(void)
{
for (Token *tok = _tokens; tok; tok = tok->next())
{
//fix Ticket #2784
if (Token::Match(tok->next(), "* & %any% ="))
{
tok->deleteNext(); //del *
tok->deleteNext(); //del &
continue;
}
if (Token::Match(tok->next(), "* ( & %any% ) ="))
{
tok->deleteNext(); //del *
tok->deleteNext(); //del (
tok->deleteNext(); //del &
tok->next()->deleteNext(); //del )
continue;
}
}
}
bool Tokenizer::tokenize(std::istream &code,
const char FileName[],
const std::string &configuration,
@ -2034,6 +2056,9 @@ bool Tokenizer::tokenize(std::istream &code,
createTokens(code);
// simplify '* & %any% =' to '%any% ='
simplifyMulAnd();
// Convert C# code
if (_files[0].find(".cs"))
{

View File

@ -114,6 +114,11 @@ public:
*/
static void deleteTokens(Token *tok);
/**
* Simplify '* & %any% =' to '%any% ='
*/
void simplifyMulAnd(void);
/**
* Get parameter name of function
* @param ftok The token for the function name in a function

View File

@ -230,6 +230,7 @@ private:
TEST_CASE(simplify_constants2);
TEST_CASE(simplify_constants3);
TEST_CASE(simplify_null);
TEST_CASE(simplifyMulAnd); // #2784
TEST_CASE(vardecl1);
TEST_CASE(vardecl2);
@ -4001,6 +4002,23 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code,true));
}
void simplifyMulAnd()
{
// (error) Resource leak
ASSERT_EQUALS(
"void f ( ) { int f ; f = open ( ) ; }",
tokenizeAndStringify(
"void f() {int f; *&f=open(); }"
)
);
ASSERT_EQUALS(
"void f ( ) { int f ; f = open ( ) ; }",
tokenizeAndStringify(
"void f() {int f; *(&f)=open(); }"
)
);
}
void vardecl1()
{
const char code[] = "unsigned int a, b;";