Skip to content

Fix #15288 (False positive: accessMoved in loop) - #9092

Open
danmar wants to merge 1 commit into
cppcheck-opensource:mainfrom
cppchecksolutions:fix-15288
Open

danmar wants to merge 1 commit into
cppcheck-opensource:mainfrom
cppchecksolutions:fix-15288

Conversation

@danmar

@danmar danmar commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

@danmar
danmar requested a review from pfultz2 November 30, 2026 16:34
@danmar danmar added the ai-assisted contains code which was written with the assistance of AI - please review extra carefully label Nov 30, 2026
Comment thread lib/vf_analyzers.cpp
return v.isImpossible() && (v.intvalue < 0 || v.intvalue > 1);
})) {
return std::any_of(tok->values().cbegin(), tok->values().cend(), [&](const ValueFlow::Value& v) {
return v.isSymbolicValue() && v.isKnown() && v.intvalue == 0 &&

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think ProgramMemory stores values as known. Also, why are we allowing symbolic values here?

Comment thread lib/vf_analyzers.cpp
// The range of a bool (not <= -1, not >= 2) does not say anything about its value, use the
// expression that the bool is known to be equal to instead
if (astIsBool(tok) && std::all_of(values->cbegin(), values->cend(), [](const ValueFlow::Value& v) {
return v.isImpossible() && (v.intvalue < 0 || v.intvalue > 1);

@pfultz2 pfultz2 Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should to filter out these values in the all_of. And when that returns false we can check the symbolic values:

auto filterValues = [&](const ValueFlow::Value& v) {
    return not isOutOfRangeImpossibleValue(tok, value);
};
bool isInt = std::count_if(values->cbegin(), values->cend(), filterValues) && std::all_of(values->cbegin(), values->cend(), [](const ValueFlow::Value& v) {
    if(filterValues(value))
        return v.isIntValue();
    return true;
});
if(isInt)
    return true;
return std::any_of(tok->values().cbegin(), tok->values().cend(), [&](const ValueFlow::Value& v) {
    return v.isSymbolicValue() && v.intvalue == 0 &&
        stateCoversCondition(v.tokvalue, pm, depth - 1);
});

I think that would be a better structure here. The isOutOfRangeImpossibleValue can work for bool here but we can extend it for other integers in the future.

Another option would be to use copy_if to filter the values.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

The PR fixes the reported false positive, but it also removes true accessMoved warnings that base reports. Every time a loop condition goes through a bool local, canSplitConditional now returns false unless the bool's known symbolic value is itself covered by the memory. That also hides real double moves:

  • Loop-invariant flag: const bool b = c; if (b) w[i] = std::move(m); moves m again on every iteration when c is true. It is no longer reported, although the same code with if (c) still is, so the two spellings now behave differently.
  • Move after the first iteration: const bool first = i == 0; if (!first) w[i] = std::move(m); in a 3-iteration loop is a real double move. It is no longer reported either.

The new test only checks the case without a condition, so it does not catch this. The two concerns pfultz2 raised (C1, C2) are still open: the code is unchanged.

Comments

# Comment By Where Status
1 ProgramMemory values are not stored as known; why allow symbolic values here @pfultz2 lib/vf_analyzers.cpp:80 ⚠️ Open
2 Filter the impossible bool range values inside the all_of and fall back to symbolic values; restructure @pfultz2 lib/vf_analyzers.cpp:77 ⚠️ Open
3 Bool locals now block splitting, so real double moves are no longer reported AI review lib/vf_analyzers.cpp:79 🆕 New (false negative)

@danmar, please fix, resolve or reply to the open and new comments: #1, #2, #3.
For #3 (false negative), it is enough to create a Trac ticket and link it here. It does not need to be fixed in this PR.

Testing

I built cppcheck and testrunner in pr/ and cppcheck in base/.

  • ./testrunner TestOther TestNullPointer TestUninitVar TestCondition TestValueFlow: 643 tests, 0 failures.
  • The full ./testrunner -q: 5436 tests, 0 failures.
  • The case from the PR (f1), and the variants !last, a non-const bool and i != 1: the false positive is gone.

The following file shows the regressions (scratch/t2.cpp):

struct W { std::vector<int> v; W flip() const; };
W w[3];
void g3(W m, bool c) {            // direct condition: still reported
    for (int i = 0; i < 2; i++) {
        if (c) w[i] = std::move(m); else w[i] = m.flip();
    }
}
void g1(W m) {
    for (int i = 0; i < 3; i++) {
        if (i == 0) w[i] = m.flip(); else w[i] = std::move(m);
    }
}
void h3(W m, bool c) {            // same as g3 through a bool local: no longer reported
    for (int i = 0; i < 2; i++) {
        const bool b = c;
        if (b) w[i] = std::move(m); else w[i] = m.flip();
    }
}
void h1(W m) {                    // 3 iterations: m is moved at i==1 and again at i==2
    for (int i = 0; i < 3; i++) {
        const bool first = i == 0;
        if (first) w[i] = m.flip(); else w[i] = std::move(m);
    }
}
void h5(W m) {                    // m is moved at i==1 and again at i==2
    for (int i = 0; i < 3; i++) {
        const bool first = i == 0;
        if (!first) w[i] = std::move(m);
    }
}
void h6(W m) {
    while (true) {
        const bool b = m.v.empty();
        if (b) w[0] = std::move(m);
    }
}

cppcheck -q --enable=warning --inconclusive --library=std --template='{line}:{column} {id} {message}' t2.cpp

== base
5:33 accessMoved Access of moved variable 'm'.
16:33 accessMoved Access of moved variable 'm'.
16:49 accessMoved Access of moved variable 'm'.
22:27 accessMoved Access of moved variable 'm'.
22:59 accessMoved Access of moved variable 'm'.
28:38 accessMoved Access of moved variable 'm'.
33:24 accessMoved Access of moved variable 'm'.
34:33 accessMoved Access of moved variable 'm'.
== pr
5:33 accessMoved Access of moved variable 'm'.
33:24 accessMoved Access of moved variable 'm'.
  • h3: the warning at 16:33 is lost, while g3, the same code without the local, is still reported at 5:33.
  • h5: the warning at 28:38 is lost, although it is a real double move.
  • h1: the warning at 22:59 is lost. It is a true positive too, because the loop runs 3 times.

The same losses appear in other variants: with i < n and with if (first) std::move(m); else m.flip();. The PR reports nothing for these, while base warned (base's 55:37 there was itself a false positive).

I did not check why the recursion through the symbolic value fails for h3: either the isKnown() filter or the missing entry for c in the program memory could cause it.


Automated review of 90f9bee by Claude. The pull request was built and tested locally.

Comment thread lib/vf_analyzers.cpp
if (astIsBool(tok) && std::all_of(values->cbegin(), values->cend(), [](const ValueFlow::Value& v) {
return v.isImpossible() && (v.intvalue < 0 || v.intvalue > 1);
})) {
return std::any_of(tok->values().cbegin(), tok->values().cend(), [&](const ValueFlow::Value& v) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

When a bool's memory only holds the impossible range values, this now returns false unless the bool's known symbolic value is covered too. In practice that hides true positives that base reports:

void h3(W m, bool c) {
    for (int i = 0; i < 2; i++) {
        const bool b = c;
        if (b) w[i] = std::move(m); else w[i] = m.flip();   // base: accessMoved 16:33, PR: nothing
    }
}
void h5(W m) {
    for (int i = 0; i < 3; i++) {
        const bool first = i == 0;
        if (!first) w[i] = std::move(m);                    // base: accessMoved, PR: nothing (moved at i==1 and i==2)
    }
}

With if (c) instead of if (b), h3 is still reported, so the result now depends on whether the condition goes through a local. Please add one of these cases as a regression test that must still warn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-assisted contains code which was written with the assistance of AI - please review extra carefully

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants