Skip to content

Commit

Permalink
Improved isVariableChangedByFunctionCall, better logic when parameter…
Browse files Browse the repository at this point in the history
… might be passed by reference
  • Loading branch information
danmar committed Feb 28, 2019
1 parent 7ccf4b9 commit 4f5a426
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
10 changes: 8 additions & 2 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,8 @@ bool isVariableChangedByFunctionCall(const Token *tok, const Settings *settings,
if (!tok)
return false;

const Token * const tok1 = tok;

// address of variable
const bool addressOf = tok->astParent() && tok->astParent()->isUnaryOp("&");

Expand Down Expand Up @@ -844,10 +846,13 @@ bool isVariableChangedByFunctionCall(const Token *tok, const Settings *settings,
tok = tok->link();
else if (Token::Match(tok->previous(), "%name% ("))
break;
else if (Token::simpleMatch(tok->previous(), "> (") && tok->previous()->link())
break;
tok = tok->previous();
}
if (!tok || tok->str() != "(")
return false;
const bool possiblyPassedByReference = (tok->next() == tok1 || Token::Match(tok1->previous(), ", %name% [,)]"));
tok = tok->previous();
if (tok && tok->link() && tok->str() == ">")
tok = tok->link()->previous();
Expand Down Expand Up @@ -879,12 +884,13 @@ bool isVariableChangedByFunctionCall(const Token *tok, const Settings *settings,
// => it is assumed that parameter is an in parameter (TODO: this is a bad heuristic)
if (!addressOf && settings && settings->library.isnullargbad(tok, 1+argnr))
return false;
// addressOf => inconclusive
if (!addressOf) {
// possible pass-by-reference => inconclusive
if (possiblyPassedByReference) {
if (inconclusive != nullptr)
*inconclusive = true;
return false;
}
// Safe guess: Assume that parameter is changed by function call
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion test/testastutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class TestAstUtils : public TestFixture {
"}";
inconclusive = false;
ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "x ) ;", &inconclusive));
// FIXME : ASSERT_EQUALS(true, inconclusive);
ASSERT_EQUALS(true, inconclusive);
}

bool nextAfterAstRightmostLeaf(const char code[], const char parentPattern[], const char rightPattern[]) {
Expand Down
2 changes: 1 addition & 1 deletion test/testnullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2536,7 +2536,7 @@ class TestNullPointer : public TestFixture {
ASSERT_EQUALS("", errout.str());

check("void f(int *p = 0) {\n"
" printf(\"%d\", p);\n"
" printf(\"%p\", p);\n"
" *p = 0;\n"
"}", true);
ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) Possible null pointer dereference if the default parameter value is used: p\n", errout.str());
Expand Down

0 comments on commit 4f5a426

Please sign in to comment.