Skip to content

Fix #15264 FP knownConditionTrueFalse: then-block treated as always escaping when only one path through a nested if returns - #9070

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

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

Conversation

@autoantwort

Copy link
Copy Markdown
Contributor

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

Regression of #15042 (4b66976).

bool g();
void use();
void f(bool a, bool b) {
    if (a) {
        bool w = false;
        if (g())
            w = true;
        if (!w || !b)
            return;
    }
    if (b && a)
        use();
}
test.cpp:11:14: style: Condition 'a' is always false [knownConditionTrueFalse]

If g() and b are true the then-block falls through, so a can be true at line 11. The same cause loses true positives, e.g. nullPointerRedundantCheck for if (!p) { ...same block... } use(*p);.

Cause: afterCondition() treats the then-block as dead when the forward traversal returns Terminate::Escape. The traversal carries the true path of the opaque g() past the inner if on a fork, and since 4b66976 the main traversal commits to the false path (absolute assumption). On the main path w stays false, the return is taken and the traversal ends with Escape. The fork (w == true) falls through, but only the main traversal's terminate and actions are returned.

Fix: ForwardTraversal records how the carried forks end:

  • a fork carried to the end of the whole traversal (Progress::Continue) or stopped because the value is modified makes the result fall through (Terminate::None), together with the fork's actions;
  • a fork that stops for another reason (bail, inconclusive, ...), and a then-path that stops in its branch or is not followed (fork limits), is unknown: if the main path escapes the result is Terminate::Bail;
  • if the traversed range itself ends with an escape (isEscapeScope) every path escapes and the result stays Escape.

Forks inside a branch or loop body only walk to the end of that block and do not count.

Tests: snippets 1, 2 and 5 (the fork-limit case at normal level) of TestCondition::knownConditionAfterConditionalEscape and TestNullPointer::nullpointerAfterConditionalEscape fail without the fix. The other new snippets (including the zeroDivCond additions) pass on main too; they guard against variants of the fix that over-reach (loop-body forks, bailing forks, fork limits).

Limitations (unchanged vs main, listed in the ticket):

  • FPs remain when the conditional escape is inside a nested range of the then-block: the then-branch of another opaque if, a while/for body, or a known-true inner if. Fixing that needs per-range tracking of the forks that fell through.
  • Two zerodivcond true positives lost since 4b66976 are not restored (one of them only at --check-level=normal, because the then-path is not followed within the fork limits).

The fix for #15266 (#9068) also touches lib/forwardanalyzer.cpp; I have it rebased onto this branch and will update whichever PR is merged second.

Verification: new tests fail without the fix, testrunner passes. The light corpus (test/cfg, samples, normal and exhaustive) is unchanged and runtime is the same or lower. On a large internal code base the only change vs main is the removed FP.

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

🤖 Generated with Claude Code

…scaping when only one path through a nested if returns

The forward traversal carries the true path of an opaque condition past
the if on a fork. Since 4b66976 the main traversal also commits to the
false path (absolute assumption) when the then-branch falls through. When
the main path later escapes, the traversal reported Terminate::Escape
although the fork fell through, so afterCondition() considered the block
dead.

A fork that is carried to the end of the whole traversal, or stops
because the value is modified, now makes the result fall through,
together with its actions. A fork that stops for another reason, or a
then-path that stops in the branch or is not followed (fork limits), is
unknown: if the main path escapes, the result is Terminate::Bail. The result stays
Terminate::Escape if the traversed range ends with an escape.

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

1 participant