Skip to content

Fix #15266 FN knownConditionTrueFalse/uninitvar: ValueFlow drops the path where a flag is set in a nested branch or loop (regression of #15042) - #9068

Draft
autoantwort wants to merge 1 commit into
cppcheck-opensource:mainfrom
autoantwort:fix-nested-fork-dropped
Draft

autoantwort wants to merge 1 commit into
cppcheck-opensource:mainfrom
autoantwort:fix-nested-fork-dropped

Conversation

@autoantwort

@autoantwort autoantwort commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Trac: https://trac.cppcheck.net/ticket/15266

bool g();
bool h();
int send();
int f() {
    int x = 0;
    bool b = false;
    if (g()) {
        if (h())
            b = true;
    }
    if (b) {
        if (x == 0)
            x = send();
    }
    return x;
}

Expected (as before 4b66976): Condition 'x==0' is always true [knownConditionTrueFalse]. Actual: no warning. The ticket also has a loop example (uninitvar FN).

Cause: since 4b66976 (#15042) the traversal assumes an undecided condition false and the then-path runs on a fork that is carried past the if with updateRange(tok->next(), end). Inside a branch or loop body end is the end of that block, so a fork carried from a nested if stops there and its path is lost after the block: here only paths with b == false survive, so if (b) is never entered.

Fix: ForwardTraversal::updateBlock() (used for branches and scopes) notes when a fork was carried to the block end and stopped there without a Break. In that case the analyzer drops the program state it learned in the block at the block end (new Analyzer::resetState(), ValueFlowAnalyzer restores the program memory from a copy of the analyzer taken before the block and updates it at the block end), so after the block the main path also stands for the lost path. Inside the block the analysis stays precise.

Limitations:

  • If the main path escapes inside the block, the then-path carried from inside it is still lost (FN only, example in the ticket).
  • With --check-level=exhaustive two generated stress files take 21 -> 27 s and 14 -> 19 s; the extra time is analysis that continues on the lost paths and yields the restored warnings. The normal check level is not affected.

This touches lib/forwardanalyzer.cpp like the fix for #15264 (#9070); whichever is merged second will be rebased (a version on top of #9070 is ready).

Verification: new tests fail without the fix, testrunner passes. On a large internal code base the only changes vs main are three restored true positives.

This PR was created by Claude Code (an AI coding agent) on behalf of @autoantwort.

🤖 Generated with Claude Code

…path where a flag is set in a nested branch or loop (regression of #15042)

The then-path of an if is carried past it on a fork, but only to the end of the current range. Inside a branch or loop body it ends at the block end, while the traversal assumed its condition false, so e.g. a branch on a flag set in a nested if or loop got no values (knownConditionTrueFalse, uninitvar and nullPointer false negatives). Now when that happens the traversal drops the program state it learned in the block at the block end, so it stands for the lost path afterwards; inside the block it stays precise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants