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 16 commits into
cppcheck-opensource:mainfrom
pfultz2:valueflow-forward-work-list
Open

pfultz2 wants to merge 16 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.

@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 fix from the earlier review (#C2) works: the cases it reported (if (w) a = 1; after a flag set by an unknown call) warn again. But the same regression is still there when the flag is set inside a loop. In if (a == 0) { int w = 0; for (...) { if (g()) w = 1; } if (w) a = 1; } return 10 / a;, base reports zerodivcond and the PR reports nothing. do/while loops and nullPointerRedundantCheck lose the warning the same way. In --debug, a at the division is possible {1,0} in base and possible 1 in the PR. I found no other differences. Warnings on test/cfg and lib/tokenize.cpp are the same, and run time is about the same.

Comments

# Comment By Where Status
1 Path that keeps the value past inner if (w) a = 1; is lost: zerodivcond/nullPointerRedundantCheck regress AI review general ✅ Fixed
2 Path that keeps the value past inner if (w) a = 1; is lost when the block is walked without splitting AI review lib/forwardanalyzer.cpp:512 ✅ Fixed
3 Flag set in a loop: the path that keeps the value is lost, zerodivcond/nullPointerRedundantCheck regress AI review lib/forwardanalyzer.cpp:904 🆕 New (false negative)

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

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

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

Earlier regression (#C2): both reproducers from the review (v1.c zerodivcond, v2.c nullPointerRedundantCheck) warn again with the PR.

Base vs PR: I compared --enable=all on test/cfg/{std.cpp,posix.c,gnu.c,qt.cpp} and lib/tokenize.cpp. The warnings are identical. Times were 339s for base and 362s for the PR. I also wrote about 30 small cases: switch, nested ifs, several flags, try/catch, goto, continue/break, lambdas, unique_ptr, and flag paths that bail on h(&a). They all gave the same results as base, except the loop cases below.

Regression: a warning is lost when the flag is set in a loop

int g();
int f(int a, int n) {
    if (a == 0) {
        int w = 0;
        for (int i = 0; i < n; i++) { if (g()) w = 1; }
        if (w) { a = 1; }
    }
    return 10 / a;
}

cppcheck -q --enable=warning --template='{line} {id}' e1.cpp

  • base: 8 zerodivcond
  • PR: no output

The same happens with do { if (g()) w = 1; } while (g());, at both --check-level=normal and exhaustive. It also happens with an extra else: if (w) { a = 1; } else { if (g()) return 0; }.

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

Null pointer version:

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

The same code without the loop (if (g()) w = 1;) warns with the PR, as the new tests expect.


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

Comment thread lib/forwardanalyzer.cpp
// returns false).
Analyzer::Result updateRangeResult(Token* start, const Token* end) {
const Progress p = updateRange(start, end, true);
const bool unchanged = p == Progress::Continue && !actions.isModified();

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

Partial is only set when this path reaches the end without a modification. When the flag is set inside a loop, w is still unknown after the loop. So the later if (w) a = 1; is a real split on this traversal. I think continueAfterBranches() then ORs the then-block's Modified into actions (actions |= action;), so unchanged is false and the path on which a keeps its value is dropped. That is my reading of the code. The regression itself is reproduced:

int g();
int f(int a, int n) {
    if (a == 0) {
        int w = 0;
        for (int i = 0; i < n; i++) { if (g()) w = 1; }
        if (w) { a = 1; }
    }
    return 10 / a;
}

Base reports 8 zerodivcond and the PR reports nothing. --debug shows a possible {1,0} in base and possible 1 in the PR. The same happens with do { if (g()) w = 1; } while (g()); and with the nullPointerRedundantCheck version (if (!p) { ... if (w) p = q; } use(*p);). This is the same kind of regression as the one in the earlier review, but for flags set in loops. The new tests only cover flags set by a plain if, so a test with a loop would help.

@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 fix works, and the regressions from the earlier reviews (C2/C3, and the loop case in C4/C5) are fixed. The one new problem is performance: at --check-level=exhaustive, functions with many flags that are set by unknown calls and then tested take about twice as long as on base. Base 15.8s vs PR 32.6s at 20 flags, and 54s vs 116s at 30 flags. It also happens without the outer if. At --check-level=normal the time is the same. I found no new false positives or false negatives. Compared with base, some results change (the #15264 FP is gone and there are two new knownConditionTrueFalse warnings), and I checked that all of these are correct.

Comments

# Comment By Where Status
1 Path that keeps the value past inner if (w) a = 1; is lost (zerodivcond/nullPointerRedundantCheck regress) AI review general ✅ Fixed
2 Same lost path when the block is walked without splitting AI review lib/forwardanalyzer.cpp:512 ✅ Fixed
3 Flag set in a loop: path that keeps the value is lost, warnings regress AI review general ✅ Fixed
4 Partial not set when the flag is set in a loop, so the path that keeps the value is dropped AI review lib/forwardanalyzer.cpp:911 ✅ Fixed
5 Exhaustive check level about 2x slower on functions with many flag/if pairs AI review lib/forwardanalyzer.cpp:910 🆕 New

@pfultz2, please fix, resolve or reply to the open and new comments: #5.

Testing

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

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

Earlier reports: the reproducers from C2/C3 (if (g()) w = 1; if (w) a = 1;) and from C4/C5 (flag set in a for loop, do/while with p = q) now give zerodivcond / nullPointerRedundantCheck with the PR, the same as base.

Base vs PR: the warnings on test/cfg/.c, lib/checkother.cpp, lib/checkcondition.cpp and lib/astutils.cpp are identical. I wrote about 20 small cases: h(&a) in the flag branch, loops in the flag branch, goto, switch on the flag, else-if chains, two complementary flag ifs, a while with break, and unique_ptr. Results differ from base only in these:

  • if (p == 0) { int w = 0; if (g()) w = 1; if (!w) return; } if (p) {}: base reports the FP knownConditionTrueFalse, the PR does not (the intended fix).
  • if (x == 0) { w...; if (!w) return; } else { return; } if (x == 0) use(x);: the PR reports a new knownConditionTrueFalse "always true". This is correct, because only x==0 gets there.

Performance (new finding): the generator writes N lines like this into one function:

int g(void);
int f(int a, int b) {
  if (a == 0) {
    int w0 = 0; if (g()) w0 = 1; if (w0) { if (b) a = 1; } else { if (g()) return 0; }
    ... (N lines like this)
  }
  return 10 / a;
}

time cppcheck -q --check-level=exhaustive pN.c:

N base PR
15 5.9s 11.7s
20 15.8s 32.6s
25 31.7s 65.6s
30 54.5s 116.5s

The slowdown is about the same without the outer if (a == 0) (base 11.3s, PR 24.3s at N=20). It is also about the same with the simple form int wI = 0; if (g()) wI = 1; if (wI) a = I+1; (base 5.4s, PR 11.1s at N=20). With --check-level=normal both take under 0.1s. I did not profile it to find which part of the new path walking costs the time.


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

Comment thread lib/forwardanalyzer.cpp
// from it for good changes it or stops (e.g. 'if (g()) w = 1; if (w) x = 1;' when g()
// returns false).
Analyzer::Result updateRangeResult(Token* start, const Token* end) {
const Progress p = updateRange(start, end, true);

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

Every generic forward now walks with split=true here, and that seems to double the time at --check-level=exhaustive on code with many flags set by unknown calls. Generated function with N lines int wI = 0; if (g()) wI = 1; if (wI) { if (b) a = I+1; } else { if (g()) return I; } inside if (a == 0) { ... } return 10 / a;:

N base PR
15 5.9s 11.7s
20 15.8s 32.6s
30 54.5s 116.5s

The slowdown is about the same without the outer if (11.3s vs 24.3s at N=20) and with the simple form if (wI) a = I+1; (5.4s vs 11.1s). The normal level is not affected. Could the path walking skip the extra work when the result of the parted paths cannot change anything, e.g. when the main path reaches the end and no fork modifies the value?

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

Labels

None yet

2 participants