Fixed #2304 (Tokenizer::simplifyKnownVariables: known strcpy parameter)

This commit is contained in:
Daniel Marjamäki 2010-12-26 20:34:07 +01:00
parent 5f3d2a7e35
commit 6178459c15
2 changed files with 37 additions and 0 deletions

View File

@ -6262,6 +6262,31 @@ bool Tokenizer::simplifyKnownVariables()
}
}
// Variable is used as 2nd parameter in function call..
if (Token::Match(tok3, ("%var% ( %any% , " + structname + " %varid% ,|)").c_str(), varid))
{
const char * const functionName[] =
{
"memcmp","memcpy","memmove",
"strcmp","strcpy","strncmp","strncpy"
};
for (unsigned int i = 0; i < (sizeof(functionName) / sizeof(*functionName)); ++i)
{
if (tok3->str() == functionName[i])
{
Token *par = tok3->tokAt(4);
if (!structname.empty())
{
par->deleteThis();
par->deleteThis();
}
par->str(value);
par->varId(valueVarId);
break;
}
}
}
// array usage
if (Token::Match(tok3, ("[(,] " + structname + " %varid% [+-*/[]").c_str(), varid))
{

View File

@ -123,6 +123,7 @@ private:
TEST_CASE(simplifyKnownVariables33); // struct variable
TEST_CASE(simplifyKnownVariables34);
TEST_CASE(simplifyKnownVariables35); // ticket #2353 - False positive: Division by zero 'if (x == 0) return 0; return 10 / x;'
TEST_CASE(simplifyKnownVariables36); // ticket #2304 - known value for strcpy parameter
TEST_CASE(simplifyKnownVariablesBailOutAssign);
TEST_CASE(simplifyKnownVariablesBailOutFor1);
TEST_CASE(simplifyKnownVariablesBailOutFor2);
@ -1898,6 +1899,17 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
void simplifyKnownVariables36()
{
// Ticket #2304
const char code[] = "void f() {"
" const char *q = \"hello\";"
" strcpy(p, q);"
"}";
const char expected[] = "void f ( ) { const char * q ; q = \"hello\" ; strcpy ( p , \"hello\" ) ; }";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
}
void simplifyKnownVariablesBailOutAssign()
{
const char code[] = "int foo() {\n"