Skip to content
6 changes: 4 additions & 2 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2646,14 +2646,16 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings &settings,
return false;

const Token *tok2 = tok;
int derefs = 0;
int derefs = 0, arrayAccess = 0;
while ((tok2->astParent() && tok2->astParent()->isUnaryOp("*")) ||
(Token::simpleMatch(tok2->astParent(), ".") && !Token::Match(tok2->astParent()->astParent(), "[(,]")) ||
(tok2->astParent() && tok2->astParent()->isUnaryOp("&") && Token::simpleMatch(tok2->astParent()->astParent(), ".") && tok2->astParent()->astParent()->originalName()=="->") ||
(Token::simpleMatch(tok2->astParent(), "[") && tok2 == tok2->astParent()->astOperand1()) ||
(Token::simpleMatch(tok2->astParent(), "(") && tok2->astParent()->isCast())) {
if (tok2->astParent() && (tok2->astParent()->isUnaryOp("*") || (astIsLHS(tok2) && tok2->astParent()->originalName() == "->" && !hasOverloadedMemberAccess(tok2))))
derefs++;
if (Token::simpleMatch(tok2->astParent(), "[") && tok2->valueType() && tok2->valueType()->pointer > 0)
arrayAccess++;
if (derefs > indirect)
break;
if (tok2->astParent() && tok2->astParent()->isUnaryOp("&") && Token::simpleMatch(tok2->astParent()->astParent(), ".") && tok2->astParent()->astParent()->originalName()=="->")
Expand Down Expand Up @@ -2688,7 +2690,7 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings &settings,
};
tok2 = skipRedundantPtrOp(tok2, tok2->astParent());

if (tok2->astParent() && tok2->astParent()->isAssignmentOp()) {
if (tok2->astParent() && tok2->astParent()->isAssignmentOp() && (indirect - derefs - arrayAccess <= 0 || tok2->astParent()->str() == "=")) {
if (astIsLHS(tok2))
return true;
// Check if assigning to a non-const lvalue
Expand Down
3 changes: 2 additions & 1 deletion lib/vf_analyzers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ struct ValueFlowAnalyzer : Analyzer {
if (inconclusive)
return Action::Read | Action::Inconclusive;
if (isVariableChanged(tok, getIndirect(tok), getSettings())) {
if (Token::Match(tok->astParent(), "*|[|.|++|--"))
if (Token::Match(tok->astParent(), "*|[|.|++|--") ||
(Token::Match(tok->astParent(), "%assign%") && tok->astParent()->str().size() > 1)) // compound assignment
return Action::Read | Action::Invalid;
// Check if its assigned to the same value
if (value && !value->isImpossible() && Token::simpleMatch(tok->astParent(), "=") && astIsLHS(tok) &&
Expand Down
2 changes: 1 addition & 1 deletion test/testnullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4933,7 +4933,7 @@ class TestNullPointer : public TestFixture {
" p += a;\n"
" *p = 0;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
ASSERT_EQUALS("[test.cpp:2:7]: (error) Pointer addition with NULL pointer. [nullPointerArithmetic]\n", errout_str());

check("int f(int *p = 0) {\n"
" if (p == 0) {\n"
Expand Down
25 changes: 25 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4300,6 +4300,20 @@ class TestOther : public TestFixture {
"}\n");
ASSERT_EQUALS("[test.cpp:3:7]: (style) Variable 'a' can be declared as const array [constVariable]\n",
errout_str());

check("void f(std::vector<int>& v) {\n"
" int *p = v.data();\n"
" p[0] += 1;\n"
"}\n"
"void g(std::vector<int*>& v) {\n"
" int** p = v.data();\n"
" p[0][0] += 1;\n"
"}\n"
"void h(std::vector<int*>& v) {\n"
" int** p = v.data();\n"
" *p[0] += 1;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void constParameterCallback() {
Expand Down Expand Up @@ -5087,6 +5101,17 @@ class TestOther : public TestFixture {
"void g(__attribute__((unused)) int *p) {}\n"
"void h(int *p) {}\n");
ASSERT_EQUALS("[test.cpp:3:13]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", errout_str());

check("void f(int *r, int s, int N) {\n"
" int *p = nullptr;\n"
" for (int j = 0; j < N; j++) {\n"
" if (p) {}\n"
" p = r;\n"
" r += s;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:10]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n",
errout_str());
}

void constArray() {
Expand Down
9 changes: 9 additions & 0 deletions test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6861,6 +6861,15 @@ class TestUninitVar : public TestFixture {
" return a[0];\n"
"}\n");
ASSERT_EQUALS("", errout_str());

valueFlowUninit("void f(int i) {\n"
" int x, y;\n"
" x += i;\n"
" y <<= i;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:5]: (error) Uninitialized variable: x [uninitvar]\n"
"[test.cpp:4:5]: (error) Uninitialized variable: y [uninitvar]\n",
errout_str());
}

void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value
Expand Down
16 changes: 16 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,22 @@ class TestValueFlow : public TestFixture {
" g(std::move(x)).f(1);\n"
"}\n";
ASSERT_EQUALS(false, testValueOfX(code, 5U, ValueFlow::Value::MoveKind::MovedVariable));

code = "void f(std::string x) {\n" // #15155
" std::string a{ std::move(x) };\n"
" x += \"abc\";\n"
"}\n";
ASSERT_EQUALS(true, testValueOfX(code, 3U, ValueFlow::Value::MoveKind::MovedVariable));

code = "struct S {\n"
" S& operator<<=(int i);\n"
" std::string m;\n"
"};\n"
"void f(S x) {\n"
" S a{ std::move(x) };\n"
" x <<= 5;\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 7U, ValueFlow::Value::MoveKind::MovedVariable));
}

void valueFlowCalculations() {
Expand Down
Loading