Skip to content

Commit 9af31ae

Browse files
committed
Fix the DACA issues
1 parent ec82405 commit 9af31ae

2 files changed

Lines changed: 90 additions & 6 deletions

File tree

‎lib/valueflow.cpp‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,8 @@ static bool hasInteriorCapture(const ValueFlow::Value& v, int depth = 4)
16341634
static ValueFlow::Value::LifetimeKind getReturnedLifetimeKind(const ValueFlow::Value& v, bool interior)
16351635
{
16361636
const ValueFlow::Value::LifetimeKind kind = v.lifetimeKind;
1637-
if (kind == ValueFlow::Value::LifetimeKind::Address && interior)
1637+
// A pointer or an iterator to a part of the object is not a pointer or an iterator to the object itself
1638+
if ((kind == ValueFlow::Value::LifetimeKind::Address || kind == ValueFlow::Value::LifetimeKind::Iterator) && interior)
16381639
return ValueFlow::Value::LifetimeKind::Object;
16391640
// The captured token is in the called function so the capture cannot be followed from the result
16401641
if ((kind == ValueFlow::Value::LifetimeKind::SubObject || kind == ValueFlow::Value::LifetimeKind::Lambda) &&
@@ -2477,7 +2478,7 @@ struct LifetimeStore {
24772478
value.lifetimeScope = ValueFlow::Value::LifetimeScope::Local;
24782479
value.tokvalue = lt.token;
24792480
value.errorPath = std::move(er);
2480-
value.lifetimeKind = getKind(tok, lt.token, type);
2481+
value.lifetimeKind = getKind(tok, lt, type);
24812482
value.setInconclusive(lt.inconclusive || inconclusive);
24822483
// Don't add the value a second time
24832484
if (std::find(tok->values().cbegin(), tok->values().cend(), value) != tok->values().cend())
@@ -2536,7 +2537,7 @@ struct LifetimeStore {
25362537
value.tokvalue = lt.token;
25372538
value.capturetok = argtok;
25382539
value.errorPath = er;
2539-
value.lifetimeKind = getKind(tok, lt.token, type);
2540+
value.lifetimeKind = getKind(tok, lt, type);
25402541
value.setInconclusive(inconclusive || lt.inconclusive);
25412542
value.lifetimeScope = ValueFlow::Value::LifetimeScope::Argument;
25422543

@@ -2572,7 +2573,7 @@ struct LifetimeStore {
25722573
value.tokvalue = lt.token;
25732574
value.capturetok = argtok;
25742575
value.errorPath = std::move(er);
2575-
value.lifetimeKind = getKind(tok, lt.token, byValKind ? byValKind(v.lifetimeKind) : type);
2576+
value.lifetimeKind = getKind(tok, lt, byValKind ? byValKind(v.lifetimeKind) : type);
25762577
value.setInconclusive(lt.inconclusive || v.isInconclusive() || inconclusive);
25772578
// Don't add the value a second time
25782579
if (std::find(tok->values().cbegin(), tok->values().cend(), value) != tok->values().cend())
@@ -2670,13 +2671,16 @@ struct LifetimeStore {
26702671
}
26712672

26722673
ValueFlow::Value::LifetimeKind getKind(const Token* tok,
2673-
const Token* lifetimeTok,
2674+
const ValueFlow::LifetimeToken& lt,
26742675
ValueFlow::Value::LifetimeKind kind) const
26752676
{
2677+
// An iterator to a member or an element is not an iterator to the object that the lifetime token refers to
2678+
if (kind == ValueFlow::Value::LifetimeKind::Iterator && !lt.addressOf)
2679+
kind = ValueFlow::Value::LifetimeKind::Object;
26762680
if (!keepKind)
26772681
return kind;
26782682
const auto it = std::find_if(tok->values().cbegin(), tok->values().cend(), [&](const ValueFlow::Value& v) {
2679-
return v.isLifetimeValue() && v.tokvalue == lifetimeTok;
2683+
return v.isLifetimeValue() && v.tokvalue == lt.token;
26802684
});
26812685
return it == tok->values().cend() ? kind : it->lifetimeKind;
26822686
}

‎test/teststl.cpp‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,6 +2453,86 @@ class TestStl : public TestFixture {
24532453
"}\n");
24542454
ASSERT_EQUALS("[test.cpp:4:7]: (error) Iterator 'g1().begin()' referring to temporary container 'g1()' is used with temporary container 'g1()'. [mismatchingContainerIterator]\n",
24552455
errout_str());
2456+
2457+
// An iterator to a member container is not an iterator to the object
2458+
check("struct A {\n"
2459+
" std::vector<int> v;\n"
2460+
" std::vector<int>::iterator find() { return v.begin(); }\n"
2461+
"};\n"
2462+
"void f(A& a) {\n"
2463+
" auto it = a.find();\n"
2464+
" if (it == a.v.end())\n"
2465+
" return;\n"
2466+
" a.v.erase(it);\n"
2467+
"}\n");
2468+
ASSERT_EQUALS("", errout_str());
2469+
2470+
check("struct A {\n"
2471+
" std::vector<int> v;\n"
2472+
" std::vector<int>::iterator find() { return v.begin(); }\n"
2473+
"};\n"
2474+
"struct B {\n"
2475+
" std::unique_ptr<A> d;\n"
2476+
" void f() {\n"
2477+
" auto it = d->find();\n"
2478+
" if (it == d->v.end())\n"
2479+
" return;\n"
2480+
" d->v.erase(it);\n"
2481+
" }\n"
2482+
"};\n");
2483+
ASSERT_EQUALS("", errout_str());
2484+
2485+
check("struct A {\n"
2486+
" std::vector<int> v;\n"
2487+
" std::vector<int>::iterator find() { return v.begin(); }\n"
2488+
"};\n"
2489+
"void f(std::map<int, A>& m) {\n"
2490+
" for (auto it = m.begin(); it != m.end(); ++it) {\n"
2491+
" A& a = it->second;\n"
2492+
" auto i = a.find();\n"
2493+
" if (i != a.v.end()) {}\n"
2494+
" }\n"
2495+
"}\n");
2496+
ASSERT_EQUALS("", errout_str());
2497+
2498+
check("struct A { std::vector<int> v; };\n"
2499+
"std::vector<int>::iterator first(std::vector<int>& x) { return x.begin(); }\n"
2500+
"void f(A& a) {\n"
2501+
" std::vector<int>& r = a.v;\n"
2502+
" auto it = first(r);\n"
2503+
" if (it == a.v.end())\n"
2504+
" return;\n"
2505+
" a.v.erase(it);\n"
2506+
"}\n");
2507+
ASSERT_EQUALS("", errout_str());
2508+
2509+
check("struct A { std::vector<int> v; };\n"
2510+
"std::vector<int>::iterator first(std::vector<int>& x) { return x.begin(); }\n"
2511+
"void f(std::vector<A>& as) {\n"
2512+
" for (A& a : as) {\n"
2513+
" auto it = first(a.v);\n"
2514+
" if (it != a.v.end()) {}\n"
2515+
" }\n"
2516+
"}\n");
2517+
ASSERT_EQUALS("", errout_str());
2518+
2519+
check("struct A { std::vector<int> v; };\n"
2520+
"std::vector<int>::iterator first(std::vector<int>& x [[clang::lifetimebound]]);\n"
2521+
"void f(A& a) {\n"
2522+
" auto it = first(a.v);\n"
2523+
" if (it == a.v.end())\n"
2524+
" return;\n"
2525+
" a.v.erase(it);\n"
2526+
"}\n");
2527+
ASSERT_EQUALS("", errout_str());
2528+
2529+
check("std::vector<int>::iterator first(std::vector<int>& x) { return x.begin(); }\n"
2530+
"void f(std::vector<int>& v, std::vector<int>& w) {\n"
2531+
" auto it = first(v);\n"
2532+
" if (it != w.end()) {}\n"
2533+
"}\n");
2534+
ASSERT_EQUALS("[test.cpp:3:21]: (error) Iterators of different containers 'v' and 'w' are used together. [mismatchingContainers]\n",
2535+
errout_str());
24562536
}
24572537

24582538
void eraseIteratorOutOfBounds() {

0 commit comments

Comments
 (0)