Skip to content

Commit 152ccdb

Browse files
committed
Fix
1 parent fd18797 commit 152ccdb

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

lib/checkautovariables.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,6 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
612612
if (tokvalue->exprId() == tok->exprId() && !(tok->variable() && tok->variable()->isArray()) &&
613613
!astIsContainerView(tok->astParent()))
614614
continue;
615-
if (tokvalue->str() == "=" && isDesignatedInitializer(tokvalue->astOperand1()))
616-
tokvalue = tokvalue->astOperand2();
617615
if ((tokvalue->variable() && !isEscapedReference(tokvalue->variable()) &&
618616
isInScope(tokvalue->variable()->nameToken(), scope)) ||
619617
isDeadTemporary(tokvalue, nullptr, mSettings->library)) {

lib/valueflow.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2861,30 +2861,39 @@ static void valueFlowLifetimeClassConstructor(Token* tok,
28612861
std::vector<const Token*> args = getArguments(tok);
28622862
if (scope->numConstructors == 0) {
28632863
auto it = scope->varlist.cbegin();
2864+
const bool hasDesignatedInitializers = !args.empty() && isDesignatedInitializer(args[0]->astOperand1());
28642865
LifetimeStore::forEach(
28652866
tokenlist,
28662867
errorLogger,
28672868
settings,
28682869
args,
28692870
"Passed to constructor of '" + t->name() + "'.",
28702871
ValueFlow::Value::LifetimeKind::SubObject,
2871-
[&](LifetimeStore& ls) {
2872-
// Skip static variable
2873-
it = std::find_if(it, scope->varlist.cend(), [](const Variable& var) {
2874-
return !var.isStatic();
2872+
[&](LifetimeStore &ls)
2873+
{
2874+
// Skip static variable
2875+
it = std::find_if(it, scope->varlist.cend(), [&](const Variable &var)
2876+
{ return !var.isStatic() && (!hasDesignatedInitializers || var.name() == ls.argtok->astOperand1()->astOperand1()->str()); });
2877+
if (it == scope->varlist.cend())
2878+
return;
2879+
if (hasDesignatedInitializers)
2880+
ls.argtok = ls.argtok->astOperand2();
2881+
const Variable &var = *it;
2882+
if (var.valueType() && var.valueType()->container && var.valueType()->container->stdStringLike && !var.valueType()->container->view)
2883+
return; // TODO: check in isLifetimeBorrowed()?
2884+
if (var.isReference() || var.isRValueReference())
2885+
{
2886+
ls.byRef(tok, tokenlist, errorLogger, settings);
2887+
}
2888+
else if (ValueFlow::isLifetimeBorrowed(ls.argtok, settings))
2889+
{
2890+
ls.byVal(tok, tokenlist, errorLogger, settings);
2891+
}
2892+
if (hasDesignatedInitializers)
2893+
it = scope->varlist.cbegin();
2894+
else
2895+
it++;
28752896
});
2876-
if (it == scope->varlist.cend())
2877-
return;
2878-
const Variable& var = *it;
2879-
if (var.valueType() && var.valueType()->container && var.valueType()->container->stdStringLike && !var.valueType()->container->view)
2880-
return; // TODO: check in isLifetimeBorrowed()?
2881-
if (var.isReference() || var.isRValueReference()) {
2882-
ls.byRef(tok, tokenlist, errorLogger, settings);
2883-
} else if (ValueFlow::isLifetimeBorrowed(ls.argtok, settings)) {
2884-
ls.byVal(tok, tokenlist, errorLogger, settings);
2885-
}
2886-
it++;
2887-
});
28882897
} else {
28892898
const Function* constructor = findConstructor(scope, tok, args);
28902899
valueFlowLifetimeUserConstructor(tok, constructor, t->name(), args, tokenlist, errorLogger, settings);

test/testautovariables.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3945,7 +3945,7 @@ class TestAutoVariables : public TestFixture {
39453945
" return a;\n"
39463946
"}\n");
39473947
ASSERT_EQUALS(
3948-
"[test.cpp:4:12] -> [test.cpp:5:12]: (error) Returning object that will be invalid when returning. [returnDanglingLifetime]\n",
3948+
"[test.cpp:4:14] -> [test.cpp:3:9] -> [test.cpp:5:12]: (error) Returning object that points to local variable 'x' that will be invalid when returning. [returnDanglingLifetime]\n",
39493949
errout_str());
39503950

39513951
check("struct A { int x; int& r};\n"

0 commit comments

Comments
 (0)