Skip to content

Fix #15228 (valueFlowLifetime: subscripted array is not a decayed pointer) - #9039

Open
danmar wants to merge 1 commit into
cppcheck-opensource:mainfrom
cppchecksolutions:fix-15228
Open

danmar wants to merge 1 commit into
cppcheck-opensource:mainfrom
cppchecksolutions:fix-15228

Conversation

@danmar

@danmar danmar commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

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).

@dmcppcheck

Copy link
Copy Markdown
Contributor

There should probably be a trac ticket for this PR. Please create it, or ask us to create it. Then the PR title should be updated.

@danmar danmar changed the title valueFlowLifetime: subscripted array is not a decayed pointer Fix #15228 (valueFlowLifetime: subscripted array is not a decayed pointer) Nov 17, 2026
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>
@danmar
danmar force-pushed the fix-15228 branch 2 times, most recently from d1ed991 to 396bb75 Compare November 17, 2026 18:24

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

I found no new problems. When the array is the left operand of [, isDecayedPointer() now returns false. That fixes the quadratic slowdown: a function with 3000 arr[i] = "..." statements took 105 s with base and 0.36 s with the PR. I compared the PR with base on many lifetime, dangling-pointer, uninitvar and multi-dimensional-array cases (a[0] of int a[2][3]) and on test/cfg, samples, externals, cli, gui and tools. The results were the same everywhere. I could not reproduce the 2 false positives that the description reports from the Debian run.

Testing

  • I built cppcheck and testrunner in both pr/ and base/.

  • The full ./testrunner -q passes: 5419 tests, 0 failures.

  • I copied the PR's test/testvalueflow.cpp into base. The new assertion fails there, at line 845 (expected 0, got 1). So the test covers the change.

  • Performance: generated void f(void){ const char *arr[3000]; arr[0]="s0"; ... arr[2999]="s2999"; } and ran cppcheck -q perf.c. It took 1m44.9s with base and 0.36s with the PR.

  • Behaviour: I wrote scratch files covering:

    • returning or storing a[0] of 2D arrays in C and C++ (std::string_view, std::vector<int*>)
    • &a[i] aliases followed by uninit reads
    • use after scope of p = a[0] and p = &a[1]
    • p = a[0] + 1
    • pointer arrays filled from malloc and then dereferenced

    Output from base and PR was identical. Some false negatives and one existing uninitvar false positive (&a[0].x) are already present in base, so the PR did not cause them.

  • Corpus comparison: ran base and PR on test/cfg and samples (exhaustive check level, --enable=warning,style --inconclusive) and on externals cli gui/*.cpp tools. There was no difference in the warnings.


Automated review of 396bb75 by Claude. The pull request was built and tested locally.

@danmar danmar added the ai-assisted contains code which was written with the assistance of AI - please review extra carefully label Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-assisted contains code which was written with the assistance of AI - please review extra carefully

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants