Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/forwardanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ namespace {
return Break();
}
}
analyzer->assume(condTok, !inElse, Analyzer::Assume::Quiet);
analyzer->assume(condTok, !inElse);
assert(!inDoWhile || Token::simpleMatch(tok, "} while ("));
if (hasElse || inDoWhile)
tok = tok->linkAt(2);
Expand Down
14 changes: 12 additions & 2 deletions lib/vf_analyzers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,12 @@ struct MultiValueFlowAnalyzer : ValueFlowAnalyzer {

void addErrorPath(const Token* tok, const std::string& s) override {
for (auto&& p:values) {
p.second.errorPath.emplace_back(tok, s);
auto& ep = p.second.errorPath;
if (std::any_of(ep.begin(), ep.end(), [&](const ErrorPathItem& epi) {
return epi.first == tok && epi.second == s;
}))
continue;
ep.emplace_back(tok, s);
}
}

Expand Down Expand Up @@ -1146,7 +1151,12 @@ struct SingleValueFlowAnalyzer : ValueFlowAnalyzer {
}

void addErrorPath(const Token* tok, const std::string& s) override {
value.errorPath.emplace_back(tok, s);
auto& ep = value.errorPath;
if (std::any_of(ep.begin(), ep.end(), [&](const ErrorPathItem& epi) {
return epi.first == tok && epi.second == s;
}))
return;
ep.emplace_back(tok, s);
}

template<class T>
Expand Down
16 changes: 8 additions & 8 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3591,7 +3591,7 @@ class TestAutoVariables : public TestFixture {
" }\n"
" *p = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:13] -> [test.cpp:2:9] -> [test.cpp:7:6]: (error) Static variable 'p' will use pointer to local variable 'a'. [danglingLifetime]\n", errout_str());
ASSERT_EQUALS("[test.cpp:5:13] -> [test.cpp:4:9] -> [test.cpp:2:9] -> [test.cpp:7:6]: (error) Static variable 'p' will use pointer to local variable 'a'. [danglingLifetime]\n", errout_str());

// #10902
check("void f() {\n"
Expand Down Expand Up @@ -4330,7 +4330,7 @@ class TestAutoVariables : public TestFixture {
" std::vector<char*> cargs = f({ \"0\", \"0\" });\n"
" (void)cargs;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:6:12] -> [test.cpp:4:47] -> [test.cpp:3:22] -> [test.cpp:1:58] -> [test.cpp:4:40] -> [test.cpp:9:34] -> [test.cpp:9:34] -> [test.cpp:10:11]: (error) Using object that is a temporary. [danglingTemporaryLifetime]\n", errout_str());
ASSERT_EQUALS("[test.cpp:6:12] -> [test.cpp:4:47] -> [test.cpp:3:22] -> [test.cpp:1:58] -> [test.cpp:4:40] -> [test.cpp:3:24] -> [test.cpp:9:34] -> [test.cpp:9:34] -> [test.cpp:10:11]: (error) Using object that is a temporary. [danglingTemporaryLifetime]\n", errout_str());

check("struct C {\n" // #9194
" const int& m;\n"
Expand Down Expand Up @@ -4692,7 +4692,7 @@ class TestAutoVariables : public TestFixture {
" }\n"
" f();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:25] -> [test.cpp:4:13] -> [test.cpp:7:5]: (error) Using lambda that captures local variable 'b' that is out of scope. [invalidLifetime]\n", errout_str());
ASSERT_EQUALS("[test.cpp:5:25] -> [test.cpp:3:11] -> [test.cpp:4:13] -> [test.cpp:7:5]: (error) Using lambda that captures local variable 'b' that is out of scope. [invalidLifetime]\n", errout_str());

check("void f(bool b) {\n"
" int* x;\n"
Expand All @@ -4703,7 +4703,7 @@ class TestAutoVariables : public TestFixture {
" x[3];\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:5:9] -> [test.cpp:4:9] -> [test.cpp:7:3]: (error) Using pointer to local variable 'y' that is out of scope. [invalidLifetime]\n",
"[test.cpp:5:9] -> [test.cpp:3:6] -> [test.cpp:4:9] -> [test.cpp:7:3]: (error) Using pointer to local variable 'y' that is out of scope. [invalidLifetime]\n",
errout_str());

check("void foo(int a) {\n"
Expand Down Expand Up @@ -4893,7 +4893,7 @@ class TestAutoVariables : public TestFixture {
" }\n"
" *p = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:9] -> [test.cpp:4:9] -> [test.cpp:7:4]: (error) Using pointer to local variable 'x' that is out of scope. [invalidLifetime]\n", errout_str());
ASSERT_EQUALS("[test.cpp:5:9] -> [test.cpp:3:7] -> [test.cpp:4:9] -> [test.cpp:7:4]: (error) Using pointer to local variable 'x' that is out of scope. [invalidLifetime]\n", errout_str());

// FP: don't warn in subfunction
check("void f(struct KEY *key) {\n"
Expand Down Expand Up @@ -4934,7 +4934,7 @@ class TestAutoVariables : public TestFixture {
" dosth();\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:5:24] -> [test.cpp:3:47] -> [test.cpp:4:26] -> [test.cpp:7:9]: (error) Using pointer to local variable 'item' that is out of scope. [invalidLifetime]\n",
"[test.cpp:5:24] -> [test.cpp:3:47] -> [test.cpp:3:47] -> [test.cpp:4:26] -> [test.cpp:7:9]: (error) Using pointer to local variable 'item' that is out of scope. [invalidLifetime]\n",
errout_str());

// #6575
Expand All @@ -4960,7 +4960,7 @@ class TestAutoVariables : public TestFixture {
" return 0;\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:5:16] -> [test.cpp:7:10] -> [test.cpp:4:13] -> [test.cpp:8:17]: (error) Using pointer to local variable 'x' that is out of scope. [invalidLifetime]\n",
"[test.cpp:5:16] -> [test.cpp:3:8] -> [test.cpp:7:10] -> [test.cpp:4:13] -> [test.cpp:8:17]: (error) Using pointer to local variable 'x' that is out of scope. [invalidLifetime]\n",
errout_str());

// #11753
Expand All @@ -4972,7 +4972,7 @@ class TestAutoVariables : public TestFixture {
" }\n"
" std::cout << s;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:26] -> [test.cpp:4:14] -> [test.cpp:7:18]: (error) Using pointer to local variable 'buff' that is out of scope. [invalidLifetime]\n", errout_str());
ASSERT_EQUALS("[test.cpp:5:26] -> [test.cpp:3:14] -> [test.cpp:4:14] -> [test.cpp:7:18]: (error) Using pointer to local variable 'buff' that is out of scope. [invalidLifetime]\n", errout_str());

check("char* f(char* dst) {\n"
" const char* src = \"abc\";\n"
Expand Down
1 change: 1 addition & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@ class TestOther : public TestFixture {
"[test.cpp:7:14]: note: Division by zero\n"
"[test.cpp:13:14]: warning: Division by zero. [zerodivcond]\n"
"[test.cpp:12:13]: note: Assignment 'j=0', assigned value is 0\n"
"[test.cpp:11:9]: note: Assuming condition is true\n"
"[test.cpp:13:14]: note: Division by zero\n", errout_str());
}

Expand Down
Loading