Fixed #1709 (char buffer that is only accessed with pointers is marker not assigned)

This commit is contained in:
Robert Reif 2010-05-26 19:22:35 +02:00 committed by Daniel Marjamäki
parent d23f63c805
commit 176b41caa5
2 changed files with 41 additions and 7 deletions

View File

@ -742,7 +742,7 @@ Variables::VariableUsage *Variables::find(unsigned int varid)
return 0;
}
static int doAssignment(Variables &variables, const Token *tok, bool pointer, bool post)
static int doAssignment(Variables &variables, const Token *tok, bool pointer, bool post, bool paren)
{
int next = 0;
@ -758,6 +758,9 @@ static int doAssignment(Variables &variables, const Token *tok, bool pointer, bo
if (post)
start++;
if (paren)
start++;
if (Token::Match(tok->tokAt(start), "&| %var%") ||
Token::Match(tok->tokAt(start), "( const| struct|union| %type% *| ) &| %var%") ||
Token::Match(tok->tokAt(start), "( const| struct|union| %type% *| ) ( &| %var%") ||
@ -1014,7 +1017,7 @@ void CheckOther::functionVariableUsage()
// check for assignment
if (written)
offset = doAssignment(variables, tok->tokAt(3), false, false);
offset = doAssignment(variables, tok->tokAt(3), false, false, false);
tok = tok->tokAt(3 + offset);
}
@ -1039,7 +1042,7 @@ void CheckOther::functionVariableUsage()
// check for assignment
if (written)
offset = doAssignment(variables, tok->tokAt(4), false, false);
offset = doAssignment(variables, tok->tokAt(4), false, false, false);
tok = tok->tokAt(4 + offset);
}
@ -1063,7 +1066,7 @@ void CheckOther::functionVariableUsage()
// check for assignment
if (written)
offset = doAssignment(variables, tok->tokAt(4), false, false);
offset = doAssignment(variables, tok->tokAt(4), false, false, false);
tok = tok->tokAt(4 + offset);
}
@ -1087,7 +1090,7 @@ void CheckOther::functionVariableUsage()
// check for assignment
if (written)
offset = doAssignment(variables, tok->tokAt(5), false, false);
offset = doAssignment(variables, tok->tokAt(5), false, false, false);
tok = tok->tokAt(5 + offset);
}
@ -1245,11 +1248,12 @@ void CheckOther::functionVariableUsage()
variables.readAll(tok->next()->varId());
// assignment
else if (Token::Match(tok, "*| ++|--| %var% ++|--| ="))
else if (Token::Match(tok, "*| (| ++|--| %var% ++|--| )| ="))
{
bool pointer = false;
bool pre = false;
bool post = false;
bool paren = false;
if (tok->str() == "*")
{
@ -1257,6 +1261,12 @@ void CheckOther::functionVariableUsage()
tok = tok->next();
}
if (tok->str() == "(")
{
paren = true;
tok = tok->next();
}
if (Token::Match(tok, "++|--"))
{
pre = true;
@ -1271,7 +1281,7 @@ void CheckOther::functionVariableUsage()
unsigned int varid1 = tok->varId();
const Token *start = tok;
tok = tok->tokAt(doAssignment(variables, tok, pointer, post));
tok = tok->tokAt(doAssignment(variables, tok, pointer, post, paren));
if (pre || post)
variables.use(varid1);

View File

@ -78,6 +78,7 @@ private:
TEST_CASE(localvar13); // ticket #1640
TEST_CASE(localvar14); // ticket #5
TEST_CASE(localvar15);
TEST_CASE(localvar16); // ticket #1709
TEST_CASE(localvaralias1);
TEST_CASE(localvaralias2); // ticket #1637
TEST_CASE(localvaralias3); // ticket #1639
@ -1120,6 +1121,29 @@ private:
}
}
void localvar16() // ticket #1709
{
{
functionVariableUsage("int foo()\n"
"{\n"
" char buf[5];\n"
" char *ptr = buf;\n"
" *(ptr++) = 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
{
functionVariableUsage("int foo()\n"
"{\n"
" char buf[5];\n"
" char *ptr = buf - 1;\n"
" *(++ptr) = 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
}
void localvaralias1()
{
functionVariableUsage("void foo()\n"