Repository navigation
Conversation
…iables) valueFlowForwardLifetime: do not forward lifetime of non-local variable past end of function For a global variable the scope body end is null so the lifetime was forwarded through the rest of the file, into unrelated functions. colobot restext.cpp: ValueFlow 615s => 5s. In 10000 Debian packages (--check-level=exhaustive) this removes 11 false positives (danglingLifetime, objectIndex) and adds no warnings. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no problems. Limiting forward analysis to the end of the enclosing function when the variable is non-local fits how ValueFlow::getEndOfExprScope is already used in the assignment branch of valueFlowForwardLifetime. When the token is at global scope (for example a global initializer), the end stays null, so the old behaviour is kept. Inside a lambda the forwarding now stops at the end of the lambda, which is correct because no code after it runs in sequence with the lambda body. My tests turned up no new false negatives or other differences in results.
Testing
-
I built
cppcheckandtestrunnerinpr/, andcppcheckinbase/. -
I ran
./testrunnerforTestValueFlow,TestAutoVariables,TestStl,TestBufferOverrun,TestNullPointer,TestUninitVarandTestOther. 0 failures in each. -
I wrote
scratch/a.cppand checked it with base and PR usingcppcheck -q --enable=all --inconclusive --check-level=exhaustive. It has true positives that involve non-local variables:invalidContaineron global vectors and strings (&gv[0],gv.begin(),gs.c_str(), followed bypush_back,clearor assignment), including inside a lambda.invalidContaineron class members, in an inline member function and in an out-of-class one.invalidContaineron a vector in a namespace.- A global pointer to an out-of-scope local (
danglingLifetime/invalidLifetime). pointerOutOfBoundson a global array.
Base and PR gave identical output.
-
I ran base and PR on
test/cfg/std.cpp,qt.cpp,wxwidgets.cpp,windows.cppandsamples/*/bad.c*with the same options. The output was identical: 1692 lines each. -
I could not reproduce the colobot timing improvement because it needs the colobot sources, and the container has no network access.
Automated review of afb5cba by Claude. The pull request was built and tested locally.
|
Test results for commit afb5cba (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9014/ Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong. |
|
@pfultz2 any opinions? This does not seem to have a big impact in general on speed. But it fixes some false positives. The reduced code for the test: |
valueFlowForwardLifetime: do not forward lifetime of non-local variable past end of function
For a global variable the scope body end is null so the lifetime was forwarded through the rest of the file, into unrelated functions.
colobot restext.cpp: ValueFlow 615s => 5s.
In 10000 Debian packages (--check-level=exhaustive) this removes 11 false positives (danglingLifetime, objectIndex) and adds no warnings.