Skip to content

Fix 15042: ValueFlow: conditionally reassigned value should not be known - #8899

Open
pfultz2 wants to merge 7 commits into
cppcheck-opensource:mainfrom
pfultz2:programmmemory-multi-values
Open

pfultz2 wants to merge 7 commits into
cppcheck-opensource:mainfrom
pfultz2:programmmemory-multi-values

Conversation

@pfultz2

@pfultz2 pfultz2 commented Sep 27, 2026

Copy link
Copy Markdown
Collaborator

This refactors ProgramMemory so it can store a list of values.

A condition such as x > 3 was recorded as the possible value 4 with a lower bound, and the executor then used it as if x were exactly 4. Nested conditions were decided wrongly, so branches were skipped and values leaked past or were lost before them. This what lead to the FP in 15042.

Now it can understand these constraints better. As new values are added, it is resolved similar to Token::addValue.

Comment thread lib/programmemory.cpp
// Shifts: calculate() rejects a negative or too large shift and a negative value
bool error = false;
result = calculate(op, edge, k, &error);
if (!rangeIsLhs || error || (op == "<<" && (result >> k) != edge))
Comment thread lib/programmemory.cpp
// Shifts: calculate() rejects a negative or too large shift and a negative value
bool error = false;
result = calculate(op, edge, k, &error);
if (!rangeIsLhs || error || (op == "<<" && (result >> k) != edge))
Comment thread lib/programmemory.cpp
// known value and does not depend on a tracked value
const Values* getStoredValues(const Token* expr) const
{
if (expr->exprId() == 0)
Comment thread lib/programmemory.cpp
const ValueFlow::Value& v = utils::as_const(*pm).at(expr->exprId());
if (v.isIntValue())
return v;
if (const ValueFlow::Value* v = pm->getValue(expr->exprId(), /*impossible*/ true)) {
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