Skip to content

Commit 61d2460

Browse files
authored
Fix #15228 (valueFlowLifetime: subscripted array is not a decayed pointer) (#9039)
1 parent 21459e9 commit 61d2460

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

‎lib/valueflow.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2842,6 +2842,9 @@ static bool isDecayedPointer(const Token *tok)
28422842
return false;
28432843
if (!tok->astParent())
28442844
return false;
2845+
// Subscript 'arr[i]': the element is accessed, the array itself is not used as a pointer
2846+
if (Token::simpleMatch(tok->astParent(), "[") && astIsLHS(tok))
2847+
return false;
28452848
if (Token::simpleMatch(tok->astParent(), "=") && astIsRHS(tok))
28462849
return true;
28472850
if (astIsPointer(tok->astParent()) && !Token::simpleMatch(tok->astParent(), "return"))

‎test/testvalueflow.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,15 @@ class TestValueFlow : public TestFixture {
836836
ASSERT_EQUALS(false, testLifetimeOfX(code, 3, "x ["));
837837
}
838838

839+
{
840+
// array of pointers is not decayed when it is subscripted
841+
const char code[] = "const char* x[10];\n"
842+
"void f() {\n"
843+
" x[0] = \"a\";\n"
844+
"}\n";
845+
ASSERT_EQUALS(false, testLifetimeOfX(code, 3, "x ["));
846+
}
847+
839848
{
840849
const char code[] = "void f() {\n"
841850
" std::vector<int> v;\n"

0 commit comments

Comments
 (0)