Skip to content

Commit 396bb75

Browse files
dmcppcheckclaude
andcommitted
valueFlowLifetime: subscripted array is not a decayed pointer
For 'arr[i]' where arr is an array of pointers, isDecayedPointer() returned true because the type of 'arr[i]' is a pointer. A lifetime value was then created and forwarded for each subscript; with many 'arr[N] = ...' statements in one function this is quadratic. spice rop3.c: ValueFlow 352s => 28s. In 10000 Debian packages (--check-level=exhaustive) this adds 11 warnings: 9 correct nullPointerOutOfMemory/nullPointerOutOfResources and 2 false positives (uninitvar, arrayIndexOutOfBoundsCond). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent fdc1942 commit 396bb75

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)