Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #10902 / #13560 FN danglingLifetime (stack address stored in static variable) #7225

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update checkautovariables.cpp
  • Loading branch information
chrchr-github authored Jan 15, 2025
commit 9cde2ac04cca343471f5ca9a5962182c046cb4ac
9 changes: 5 additions & 4 deletions lib/checkautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,13 +645,13 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
const Token* nextTok = nextAfterAstRightmostLeaf(tok->astTop());
if (!nextTok)
nextTok = tok->next();
if (var && !var->isLocal() && !var->isArgument() && !(val.tokvalue && val.tokvalue->variable() && val.tokvalue->variable()->isStatic()) &&
if (var && (!var->isLocal() || var->isStatic()) && !var->isArgument() && !(val.tokvalue && val.tokvalue->variable() && val.tokvalue->variable()->isStatic()) &&
!isVariableChanged(nextTok,
tok->scope()->bodyEnd,
var->declarationId(),
var->isGlobal(),
*mSettings)) {
errorDanglngLifetime(tok2, &val);
errorDanglngLifetime(tok2, &val, var->isLocal());
break;
}
}
Expand Down Expand Up @@ -721,12 +721,13 @@ void CheckAutoVariables::errorDanglingTemporaryLifetime(const Token* tok, const
inconclusive ? Certainty::inconclusive : Certainty::normal);
}

void CheckAutoVariables::errorDanglngLifetime(const Token *tok, const ValueFlow::Value *val)
void CheckAutoVariables::errorDanglngLifetime(const Token *tok, const ValueFlow::Value *val, bool isStatic)
{
const bool inconclusive = val ? val->isInconclusive() : false;
ErrorPath errorPath = val ? val->errorPath : ErrorPath();
std::string tokName = tok ? tok->expressionString() : "x";
std::string msg = "Non-local variable '" + tokName + "' will use " + lifetimeMessage(tok, val, errorPath);
std::string msg = isStatic ? "Static" : "Non-local";
msg += " variable '" + tokName + "' will use " + lifetimeMessage(tok, val, errorPath);
errorPath.emplace_back(tok, "");
reportError(errorPath, Severity::error, "danglingLifetime", msg + ".", CWE562, inconclusive ? Certainty::inconclusive : Certainty::normal);
}
Expand Down