Fixed #2815 (FP: Null pointer dereference error about a member)

This commit is contained in:
Daniel Marjamäki 2011-06-23 16:58:01 +02:00
parent 07507a4e1d
commit 2d1b7285d2
2 changed files with 23 additions and 0 deletions

View File

@ -6870,6 +6870,14 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
if (tok3->str() == "do")
break;
// Stop if unknown function call is seen
// If the variable is a global or a member variable it might be
// changed by the function call
if (tok3->str() == ")" && tok3->link() &&
Token::Match(tok3->link()->tokAt(-2), "[;{}] %var% (") &&
!Token::Match(tok3->link()->previous(), "if|for|while|switch"))
break;
// Stop if something like 'while (--var)' is found
if (tok3->str() == "for" || tok3->str() == "while" || tok3->str() == "do")
{

View File

@ -141,6 +141,7 @@ private:
TEST_CASE(simplifyKnownVariablesBailOutConditionalIncrement);
TEST_CASE(simplifyKnownVariablesBailOutSwitchBreak); // ticket #2324
TEST_CASE(simplifyKnownVariablesFloat); // #2454 - float variable
TEST_CASE(simplifyKnownVariablesClassMember); // #2815 - value of class member may be changed by function call
TEST_CASE(varid1);
TEST_CASE(varid2);
@ -2268,6 +2269,20 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code,true));
}
void simplifyKnownVariablesClassMember()
{
// Ticket #2815
const char code[] = "char *a;\n"
"void f(const char *s) {\n"
" a = NULL;\n"
" x();\n"
" memcpy(a, s, 10);\n" // <- don't simplify "a" here
"}\n";
const std::string s(tokenizeAndStringify(code, true));
ASSERT_EQUALS(true, s.find("memcpy ( a , s , 10 ) ;") != std::string::npos);
}
std::string tokenizeDebugListing(const std::string &code, bool simplify = false)