From 396bb75d6b32cba2f8170953b0265b0ab08e762d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 4 Nov 2026 17:28:04 +0100 Subject: [PATCH] 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 --- lib/valueflow.cpp | 3 +++ test/testvalueflow.cpp | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 7f4cd5584e8..b2a5798795d 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2842,6 +2842,9 @@ static bool isDecayedPointer(const Token *tok) return false; if (!tok->astParent()) return false; + // Subscript 'arr[i]': the element is accessed, the array itself is not used as a pointer + if (Token::simpleMatch(tok->astParent(), "[") && astIsLHS(tok)) + return false; if (Token::simpleMatch(tok->astParent(), "=") && astIsRHS(tok)) return true; if (astIsPointer(tok->astParent()) && !Token::simpleMatch(tok->astParent(), "return")) diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index c7f0cd63eb0..dfbb98ad72c 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -836,6 +836,15 @@ class TestValueFlow : public TestFixture { ASSERT_EQUALS(false, testLifetimeOfX(code, 3, "x [")); } + { + // array of pointers is not decayed when it is subscripted + const char code[] = "const char* x[10];\n" + "void f() {\n" + " x[0] = \"a\";\n" + "}\n"; + ASSERT_EQUALS(false, testLifetimeOfX(code, 3, "x [")); + } + { const char code[] = "void f() {\n" " std::vector v;\n"