Skip to content

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

Open
pfultz2 wants to merge 13 commits into
cppcheck-opensource:mainfrom
pfultz2:valueflow-forward-work-list
Open

pfultz2 wants to merge 13 commits into
cppcheck-opensource:mainfrom
pfultz2:valueflow-forward-work-list

Conversation

@pfultz2

@pfultz2 pfultz2 commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

@dmcppcheck

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved.

@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 loses warnings that the base build reports. Take a then block that is walked on a fork, holds an if on a flag that an earlier unknown call set, and changes the tracked variable in that if. The path on which the variable keeps its value is dropped. In if (a == 0) { int w = 0; if (g()) w = 1; if (w) a = 1; } return 10 / a; the path with g() false reaches 10 / a with a == 0. Base reports zerodivcond for it and the PR reports nothing. In --debug, a at the division is possible {1,0} in base and possible 1 in the PR. The same happens to nullPointerRedundantCheck. This is the same kind of code that the PR targets (a flag set by an unknown call inside the outer then block), so the fix for the false positive costs true positives here. The tests do not cover it. I did not find the root cause. Removing the new updateState(ifEnd->next()) call does not fix it.

Apart from this I found no problems: the new tests pass, warnings on test/cfg, samples and cppcheck's own sources are unchanged, and there is no slowdown.

Comments

# Comment By Where Status
1 Path that keeps the value past an inner if (w) a = 1; is lost: zerodivcond/nullPointerRedundantCheck regress AI review lib/forwardanalyzer.cpp:510 🆕 New (false negative)

@pfultz2, please fix, resolve or reply to the open and new comments: #1.
For #1 (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

Built: make -j8 cppcheck testrunner in pr/ and make cppcheck in base/. Both built without problems.

Unit tests: ./testrunner TestCondition TestNullPointer TestOther TestValueFlow TestUninitVar TestBufferOverrun ran 847 tests: 0 failed, 171 todos.

Comparing base and PR:

  • test/cfg/.c, samples/ (exhaustive) and lib/.cpp + cli/.cpp: the warnings are the same.
  • Performance: I made generated files with up to 40 split flags and sequential or nested ifs with returns. Run times at normal and exhaustive level are the same or slightly lower with the PR (40 flags, exhaustive: base 59.5s, PR 48.2s).
  • My own cases for the new behaviour (switch, loops with continue/break, goto, else blocks, uninit vars) gave the same results, apart from the regression below. One more difference: in one uninitialized-variable case the warning now comes from a different check (legacyUninitvar error in base, uninitvar warning in the PR). Both are correct.

Regression (missed warning):

int g(void);
int f(int a) {
    if (a == 0) {
        int w = 0;
        if (g()) w = 1;
        if (w) { a = 1; }
    }
    return 10 / a;
}

cppcheck -q --enable=warning --template='{line} {id}' v1.c

  • base: 7 zerodivcond ("Either the condition 'a==0' is redundant or there is division by zero")
  • PR: no output, at both --check-level=normal and exhaustive

--debug at line 7: base a possible {1,0}, PR a possible 1.

The warning is lost in these variants too:

  • if (w) { a = 1; } else { if (h()) return 0; }
  • if (!w) { if (h()) return 0; } else { a = 1; }
  • if (w) { a = 1; } if (!w) { if (h()) return 0; }

if (!w) { if (h()) return 0; }, which does not change a, still warns.

The same happens with null pointers:

int g(void);
void use(int);
void f(int *p, int *q) {
    if (!p) {
        int w = 0;
        if (g()) w = 1;
        if (w) { p = q; }
    }
    use(*p);
}
  • base: 9 nullPointerRedundantCheck
  • PR: no output

I tried commenting out the new analyzer->updateState(ifEnd->next()) in updateIfBranches and rebuilding. The warning is still lost, so that call is not the cause.


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

Comment thread lib/forwardanalyzer.cpp
actions |= prevActions;
Outcome outer = std::move(parted);
parted = Outcome{};
const Progress p = updateRange(endBlock->link(), endBlock, false, depth);

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

Here a block is walked without splitting, and the path on which the tracked variable keeps its value gets lost. That path goes through an if inside the block that changes the variable.

int g(void);
int f(int a) {
    if (a == 0) {
        int w = 0;
        if (g()) w = 1;
        if (w) { a = 1; }
    }
    return 10 / a;
}

Base reports zerodivcond at line 8 and the PR reports nothing. --debug shows a possible {1,0} before and a possible 1 after. The g() false path still reaches the division with a == 0.

The same happens to nullPointerRedundantCheck with if (!p) { int w = 0; if (g()) w = 1; if (w) p = q; } use(*p);. It also happens when the if (w) has an else like else { if (h()) return 0; }.

The flag/unknown-call pattern is the one this PR is about, so it would be worth adding tests where the inner block changes the tracked variable and one path keeps the old value. Removing the new updateState(ifEnd->next()) does not fix it.

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