Skip to content

Fix #13327 FN mismatchingContainers for struct member and c_str() #7024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 8, 2024
Merged
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
24 changes: 23 additions & 1 deletion lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,14 +725,36 @@ static bool isSameIteratorContainerExpression(const Token* tok1,
return false;
}

// First it groups the lifetimes together using std::partition with the lifetimes that refer to the same token or token of a subexpression.
// Then it finds the lifetime in that group that refers to the "highest" parent using std::min_element and adds that to the vector.
static std::vector<ValueFlow::Value> pruneLifetimes(std::vector<ValueFlow::Value> lifetimes)
{
std::vector<ValueFlow::Value> result;
auto start = lifetimes.begin();
while (start != lifetimes.end())
{
const Token* tok1 = start->tokvalue;
auto it = std::partition(start, lifetimes.end(), [&](const ValueFlow::Value& v) {
const Token* tok2 = v.tokvalue;
return astHasToken(tok1, tok2) || astHasToken(tok2, tok1);
});
auto root = std::min_element(start, it, [](const ValueFlow::Value& x, const ValueFlow::Value& y) {
return x.tokvalue != y.tokvalue && astHasToken(x.tokvalue, y.tokvalue);
});
result.push_back(*root);
start = it;
}
return result;
}

static ValueFlow::Value getLifetimeIteratorValue(const Token* tok, MathLib::bigint path = 0)
{
auto findIterVal = [](const std::vector<ValueFlow::Value>& values, const std::vector<ValueFlow::Value>::const_iterator beg) {
return std::find_if(beg, values.cend(), [](const ValueFlow::Value& v) {
return v.lifetimeKind == ValueFlow::Value::LifetimeKind::Iterator;
});
};
std::vector<ValueFlow::Value> values = ValueFlow::getLifetimeObjValues(tok, false, path);
std::vector<ValueFlow::Value> values = pruneLifetimes(ValueFlow::getLifetimeObjValues(tok, false, path));
auto it = findIterVal(values, values.begin());
if (it != values.end()) {
auto it2 = findIterVal(values, it + 1);
Expand Down
22 changes: 22 additions & 0 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class TestStl : public TestFixture {
TEST_CASE(iterator28); // #10450
TEST_CASE(iterator29);
TEST_CASE(iterator30);
TEST_CASE(iterator31);
TEST_CASE(iteratorExpression);
TEST_CASE(iteratorSameExpression);
TEST_CASE(mismatchingContainerIterator);
Expand Down Expand Up @@ -1900,6 +1901,27 @@ class TestStl : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void iterator31()
{
check("struct S {\n" // #13327
" std::string a;\n"
"};\n"
"struct T {\n"
" S s;\n"
"};\n"
"bool f(const S& s) {\n"
" std::string b;\n"
" return s.a.c_str() == b.c_str();\n"
"}\n"
"bool g(const T& t) {\n"
" std::string b;\n"
" return t.s.a.c_str() == b.c_str();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:9]: (error) Iterators of different containers 's.a' and 'b' are used together.\n"
"[test.cpp:13]: (error) Iterators of different containers 't.s.a' and 'b' are used together.\n",
errout_str());
}

void iteratorExpression() {
check("std::vector<int>& f();\n"
"std::vector<int>& g();\n"
Expand Down
Loading