Skip to content

Commit 7e562f0

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 b60f304 commit 7e562f0

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
@@ -2835,6 +2835,9 @@ static bool isDecayedPointer(const Token *tok)
28352835
return false;
28362836
if (!tok->astParent())
28372837
return false;
2838+
// Subscript 'arr[i]': the element is accessed, the array itself is not used as a pointer
2839+
if (Token::simpleMatch(tok->astParent(), "[") && astIsLHS(tok))
2840+
return false;
28382841
if (Token::simpleMatch(tok->astParent(), "=") && astIsRHS(tok))
28392842
return true;
28402843
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
@@ -834,6 +834,15 @@ class TestValueFlow : public TestFixture {
834834
ASSERT_EQUALS(false, testLifetimeOfX(code, 3, "x ["));
835835
}
836836

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

0 commit comments

Comments
 (0)