diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 64a48285fdc..68e989c0738 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2591,7 +2591,7 @@ static void valueFlowLifetimeFunction(Token *tok, const TokenList &tokenlist, Er std::vector args = getArguments(tok); if (iArg > 0 && iArg <= args.size()) { const Token* varTok = args[iArg - 1]; - if (varTok->variable() && varTok->variable()->isLocal()) + if (varTok->variable() && varTok->variable()->isLocal() && varTok->variable()->isArray()) LifetimeStore{ varTok, "Passed to '" + tok->str() + "'.", ValueFlow::Value::LifetimeKind::Address }.byRef( tok->next(), tokenlist, errorLogger, settings); } diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 65f198dcbab..0c8addb3b94 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -908,6 +908,19 @@ class TestValueFlow : public TestFixture { ASSERT_EQUALS(true, lifetimes.size() == 1); ASSERT_EQUALS(true, lifetimes.front() == "a"); } + { + const char code[] = "void f(const char *s, size_t len) {\n" + " char buf[10];\n" + " {\n" + " char *tmp = buf;\n" + " s = strcpy(tmp, s);\n" + " }\n" + " if (s[len] == '\0') {}\n" + "}\n"; + lifetimes = lifetimeValues(code, "s ["); + ASSERT_EQUALS(true, lifetimes.size() == 1); + ASSERT_EQUALS(true, lifetimes.front() == "buf"); + } } void valueFlowArrayElement() {