Skip to content

Fix #15265 FP uninitvar: ProgramMemoryState::assume() drops a condition on a variable assigned an unknown value - #9067

Draft
autoantwort wants to merge 1 commit into
cppcheck-opensource:mainfrom
autoantwort:fix-uninit-ternary
Draft

autoantwort wants to merge 1 commit into
cppcheck-opensource:mainfrom
autoantwort:fix-uninit-ternary

Conversation

@autoantwort

Copy link
Copy Markdown
Contributor

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

int calcA();
int calcB(double *r);
void print(double d);
void f() {
    double sq;
    int err = calcA();
    if (err == 0)
        err = calcB(&sq);
    print(err == 0 ? sq : 0.0);
}
test.cpp:9:22: warning: Uninitialized variable: sq [uninitvar]

On the path where err == 0 is false sq is not set, but then sq is not read either.

Cause: int err = calcA(); stores err in the program memory as an unknown value. ProgramMemoryState::assume() applies the condition (err == 0 is false) to a copy of the state and stores it with ProgramMemoryState::replace(), which since 35dc425 (#13930) uses skipUnknown = true and so keeps the unknown value of err: the condition is lost. 4b66976 (#15042) made this visible in more cases (e.g. when the read is inside another if).

Fix: assume() stores the state with the condition applied without skipUnknown. Exception: when the true path is assumed again at the end of its block (origin != nullptr, to carry it past the if) skipUnknown stays, otherwise the path past the if knows values that the forward analysis does not lower when only other paths modify the tracked value (new knownConditionTrueFalse FP, covered by TestCondition::knownConditionAfterIfNull).

Not fixed here:

  • The legacyUninitvar error in the second example of the ticket.
  • The knownConditionTrueFalse FP shown in the ticket already exists without this change when p is a parameter (since 4b66976); that is a separate issue of the forward analysis.

Verification: new tests fail without the fix, testrunner passes. On a large internal code base (checked together with the fixes for #15264, #15266 and #15267) the only change from this fix 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

…on on a variable assigned an unknown value

A variable assigned an unknown value is stored in the program memory as
unknown. Since 35dc425 (#13930) ProgramMemoryState::replace() keeps an
unknown value (skipUnknown), and assume() stores the state with the
condition applied through it, so the condition is dropped: on the false
path of 'if (err == 0)' err is not known to be non-zero. 4b66976
(#15042) made this visible in more cases.

The true path that is assumed again at the end of its block keeps an
unknown value as before: with the condition, the path past the if knows
values that the forward analysis does not lower when only other paths
modify the tracked value (knownConditionTrueFalse FP).

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.

1 participant