Repository navigation
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
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Trac: https://trac.cppcheck.net/ticket/15266
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
ifwithupdateRange(tok->next(), end). Inside a branch or loop bodyendis the end of that block, so a fork carried from a nestedifstops there and its path is lost after the block: here only paths withb == falsesurvive, soif (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 aBreak. In that case the analyzer drops the program state it learned in the block at the block end (newAnalyzer::resetState(),ValueFlowAnalyzerrestores 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:
--check-level=exhaustivetwo 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.cpplike 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,
testrunnerpasses. 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