Skip to content

Commit 8f50adc

Browse files
committed
Fix #14838 FP invalidLifetime, objectIndex with copy to pointer alias
1 parent 9becbb6 commit 8f50adc

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

lib/valueflow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2591,7 +2591,7 @@ static void valueFlowLifetimeFunction(Token *tok, const TokenList &tokenlist, Er
25912591
std::vector<const Token*> args = getArguments(tok);
25922592
if (iArg > 0 && iArg <= args.size()) {
25932593
const Token* varTok = args[iArg - 1];
2594-
if (varTok->variable() && varTok->variable()->isLocal())
2594+
if (varTok->variable() && varTok->variable()->isLocal() && varTok->variable()->isArray())
25952595
LifetimeStore{ varTok, "Passed to '" + tok->str() + "'.", ValueFlow::Value::LifetimeKind::Address }.byRef(
25962596
tok->next(), tokenlist, errorLogger, settings);
25972597
}

test/testvalueflow.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,19 @@ class TestValueFlow : public TestFixture {
908908
ASSERT_EQUALS(true, lifetimes.size() == 1);
909909
ASSERT_EQUALS(true, lifetimes.front() == "a");
910910
}
911+
{
912+
const char code[] = "void f(const char *s, size_t len) {\n"
913+
" char buf[10];\n"
914+
" {\n"
915+
" char *tmp = buf;\n"
916+
" s = strcpy(tmp, s);\n"
917+
" }\n"
918+
" if (s[len] == '\0') {}\n"
919+
"}\n";
920+
lifetimes = lifetimeValues(code, "s [");
921+
ASSERT_EQUALS(true, lifetimes.size() == 1);
922+
ASSERT_EQUALS(true, lifetimes.front() == "buf");
923+
}
911924
}
912925

913926
void valueFlowArrayElement() {

0 commit comments

Comments
 (0)