Repository navigation
Fix #15203 FN constParameterPointer (push_back() of dereferenced pointer) - #9016
chrchr-github wants to merge 7 commits into
Conversation
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no new problems. The change to pindirect only takes away the dereferences that isVariableChanged() has already walked through. It removes the reported false negative and also gives new correct constVariablePointer warnings for local pointers. Base and PR give the same results on the test/cfg files and on part of cppcheck's own sources, and I could not find a false positive. A few closely related forms are still not detected, in base and in the PR: v.push_back(p[0]), v.push_back(*(p + 1)), v.emplace_back(*p) and v.push_back(s->x). When derefs > indirect (for example indirect == 0 and f(*p)), pindirect can now become negative. Library::getArgDirection() treats a negative value as DIR_UNKNOWN, which is the conservative answer. I saw no wrong result from it.
Testing
Builds and tests
- I built
cppcheckandtestrunnerinpr/, andcppcheckinbase/. ./testrunner TestOther TestUninitVar TestNullPointer TestAstUtils TestValueFlow TestVarID TestCondition: 878 tests, 0 failed.- The full
./testrunner -q: 5414 tests, 0 failed.
Comparing base and PR on existing code
test/cfg/*.c|cpp, each with its own library,--enable=all --inconclusive --check-library: the output is identical.lib/token.cpp,lib/checkother.cpp,lib/library.cpp,lib/symboldatabase.cpp,cli/andsamples/, with--enable=style --inconclusive: the output is identical.
My own test files in scratch/ (a.cpp, b.cpp, c.cpp), run with cppcheck -q --enable=style --inconclusive:
- New, correct warnings in the PR:
void f1(std::vector<int>& v, int* p) { v.push_back(*p); }givesconstParameterPointer.int* p = &x; v.push_back(*p);andint* r = p; v.push_back(*r);giveconstVariablePointer.v.push_back((*s).x)givesconstParameterPointer.
- No new false positives in cases where the pointee really is modified, for example:
void g(int*&); void f(int** p){ g(*p); }h(*p)andh(**p)withh(int&)g3(*s)withg3(S&)out.swap(*p)std::strcpy(*s, "a")(*s).m()std::swap(*p, *p)
- The uninitvar warning on
v.push_back(*p)withp = &xandxuninitialised is still reported. - Still not detected, in both base and PR:
v.push_back(p[0]),v.push_back(*(p + 1)),v.emplace_back(*p)andv.push_back(s->x).
Automated review of 63dda39 by Claude. The pull request was built and tested locally.
|
Test results for commit 63dda39 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9016/ 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. |
|
This pull request has merge conflicts that must be resolved. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The PR causes a new false positive. When the container stores std::reference_wrapper<T> (for example std::vector<std::reference_wrapper<int>>), v.push_back(*p) keeps a non-const reference to *p. The PR now reports constParameterPointer or constVariablePointer for p in that case. Following the suggestion breaks the build: g++ gives no matching function for call to 'std::vector<std::reference_wrapper<int> >::push_back(const int&)'.
The cause: with pindirect now 0, isVariableChangedByFunctionCall() uses the direction="in" indirect="0" entry for push_back/push_front/push in std.cfg, and that entry does not cover containers of reference_wrapper. Before the PR, the lookup with indirect 1 gave an unknown direction, which was the conservative answer.
I found no other new false positives. Apart from this, the PR gives the expected new warnings.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | FP for push_back(*p) into a container of std::reference_wrapper | AI review | lib/astutils.cpp:2797 |
🆕 New |
@chrchr-github, please fix, resolve or reply to the open and new comments: #1.
Testing
Builds and tests
- I built
cppcheckandtestrunnerinpr/, andcppcheckinbase/. ./testrunner TestOther: 0 failed.
False positive found (scratch/t4.cpp):
#include <vector>
#include <functional>
void f(std::vector<std::reference_wrapper<int>>& v, int* p) {
v.push_back(*p);
}
void g(std::vector<std::reference_wrapper<int>>& v) {
int x = 0;
int* p = &x;
v.push_back(*p);
}Command: cppcheck -q --enable=style --inconclusive --template='{file}:{line}:{column} {id} {message}' t4.cpp
- base: no output.
- PR:
t4.cpp:3:58 constParameterPointer Parameter 'p' can be declared as pointer to const
t4.cpp:8:10 constVariablePointer Variable 'p' can be declared as pointer to const
The suggestion does not compile. After changing the parameter to const int* p, g++ reports error: no matching function for call to 'std::vector<std::reference_wrapper<int> >::push_back(const int&)'.
The same false positive appears, only with the PR, for:
std::deque<std::reference_wrapper<int>>::push_front(*p)std::stack<std::reference_wrapper<int>>::push(*p)std::vector<std::reference_wrapper<S>>::push_back(*p), whereSis a struct
It also appears when the stored reference is later used to write through it, for example v[0].get() = 2; (scratch/t3.cpp f11).
Other cases: base and PR give the same results
- Pointer-to-pointer cases:
g(*pp)withg(int*),g(**pp)withg(int&),g7(**ppp)withg7(int*&),memset(*b, ...)andv.push_back(*pp). - Constructors that keep a reference:
A a(*p)andA a{*p}. std::ref(*p),std::tie(*p, *q), templatetf(*p), a lambda,std::function,std::forward, astd::pair<int&, int>, andfread(&*p, ...).- The knownCondition cases in
scratch/t2.cpp.
Automated review of a007659 by Claude. The pull request was built and tested locally.
| while (Token::Match(ptok->astParent(), ".|::")) | ||
| ptok = ptok->astParent(); | ||
| int pindirect = indirect; | ||
| int pindirect = indirect - derefs; |
There was a problem hiding this comment.
AI review
With pindirect now reduced to 0, v.push_back(*p) is looked up with indirect 0 in std.cfg. There push_back/push_front/push are direction="in" indirect="0", so the argument counts as unchanged. That does not hold when the element type is std::reference_wrapper<T>: the container keeps a non-const reference to *p.
#include <vector>
#include <functional>
void f(std::vector<std::reference_wrapper<int>>& v, int* p) {
v.push_back(*p);
}The PR reports constParameterPointer for p (base reports nothing). With const int* p the code no longer compiles (no matching function for call to ... push_back(const int&)). The local-variable form gives constVariablePointer, and deque::push_front and stack::push give the same false positive.
Possible fixes: keep the conservative answer when the container's element type is a reference_wrapper (or anything that is not a plain value type), or add a test that covers this case.
There was a problem hiding this comment.
Ticket for std::reference_wrapper FP: https://trac.cppcheck.net/ticket/15103
|
Test results for commit a007659 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9016/ 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. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The PR adds a new false positive with std::begin(*p) / std::end(*p). If the iterator that comes back is used to write, the PR still reports constParameterPointer (or constVariablePointer) for p. Examples are std::fill(std::begin(*p), std::end(*p), 0), std::sort(std::begin(*p), std::end(*p)) and *std::begin(*p) = 1. Base reports nothing for this code, and the suggested const does not compile.
The cause is the same as for the reference_wrapper problem in C5. With pindirect now 0, the direction="in" indirect="0" entry for std::begin/std::end in std.cfg is taken as "not changed". That entry ignores the start-iterator yield, which gives write access. The same code with a reference parameter (std::fill(std::begin(v), std::end(v), 0)) gives no warning.
Apart from this, the PR gives the expected new warnings, and I found no other new false positives. The new std::max(..., 0) clamp makes the result match base when derefs > indirect.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | FP for push_back(*p) into a container of std::reference_wrapper | AI review | lib/astutils.cpp:2797 |
🎫 Ticket #15103 |
| 2 | New FP: std::begin(*p)/std::end(*p) used to write gives constParameterPointer | AI review | lib/astutils.cpp:2797 |
🆕 New |
@chrchr-github, please fix, resolve or reply to the open and new comments: #2.
Testing
Builds and tests
- I built
cppcheckandtestrunnerinpr/, andcppcheckinbase/. ./testrunner TestOther TestAstUtils TestUninitVar TestNullPointer TestCondition TestStl: 0 failed.
False positive found (scratch/c.cpp):
#include <vector>
#include <algorithm>
#include <iterator>
#include <array>
void f1(std::vector<int>* p) { std::fill(std::begin(*p), std::end(*p), 0); }
void f2(std::vector<int>* p) { *std::begin(*p) = 1; }
void f3(std::vector<int>* p) { auto it = std::begin(*p); *it = 1; }
void f4(std::vector<int>* p) { std::sort(std::begin(*p), std::end(*p)); }
void f7(std::array<int,3>* p) { std::sort(std::begin(*p), std::end(*p)); }
void f8(std::vector<int>* p) { for (auto it = std::begin(*p); it != std::end(*p); ++it) *it = 0; }Command: cppcheck -q --enable=style --inconclusive --template='{line}:{id} {message}' c.cpp
- base: no
constParameterPointerwarnings. - PR: one
constParameterPointerper function, for example5:constParameterPointer Parameter 'p' can be declared as pointer to const. It fires on lines 5, 6, 7, 8, 11 and 12 of the original file.
The local-variable form also gives a warning only in the PR (scratch/e.cpp):
void g(std::vector<int>& v) { std::vector<int>* p = &v; *std::begin(*p) = 1; }PR output: 4:constVariablePointer Variable 'p' can be declared as pointer to const.
After changing the parameter to const std::vector<int>* p, g++ -fsyntax-only fails with error: assignment of read-only location '* __first'.
The reference version, void f1(std::vector<int>& v) { std::fill(std::begin(v), std::end(v), 0); }, gives no warning in the PR.
Other cases: base and PR give the same results
g2(p->x),g1(&p->x),g2(*p->q),g1(*pp),g2(**pp),g4(*p),(*p).m()andp->s.append().std::getline(is, *p),fgets(*p, ...),sprintf(*p, ...),s.swap(*p),std::swap(s, *p),std::move(*p)intopush_back/emplace_back,std::sort(p->begin(), p->end())andmemcpy(&p->x, ...).
New warnings that are correct
v.push_back(b ? *p : 0),v.push_back((long)*p),scanf("%d", &*p)andv.push_back(&*p)into avector<int*>.
Automated review of a3ddba5 by Claude. The pull request was built and tested locally.
| while (Token::Match(ptok->astParent(), ".|::")) | ||
| ptok = ptok->astParent(); | ||
| int pindirect = indirect; | ||
| int pindirect = std::max(indirect - derefs, 0); |
There was a problem hiding this comment.
AI review
With pindirect now reduced to 0, std::begin(*p) / std::end(*p) is looked up as direction="in" indirect="0" in std.cfg, so the argument counts as unchanged. The yielded start iterator gives write access, though:
#include <vector>
#include <algorithm>
#include <iterator>
void f1(std::vector<int>* p) { std::fill(std::begin(*p), std::end(*p), 0); }
void f2(std::vector<int>* p) { *std::begin(*p) = 1; }
void f4(std::vector<int>* p) { std::sort(std::begin(*p), std::end(*p)); }The PR reports constParameterPointer for each of these (base reports nothing). The local-variable form gives constVariablePointer. With const std::vector<int>* p, g++ fails with assignment of read-only location.
The reference version, std::fill(std::begin(v), std::end(v), 0), gives no warning, so the *p path seems to skip a check that the reference path does. The container yield (start-iterator/iterator/buffer) of the called library function should still be taken into account after the dereference is stripped.
|
Test results for commit a3ddba5 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9016/ 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. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The new direction="inout" on std::begin/std::end/std::rbegin/std::rend in std.cfg causes a false negative. With it, cppcheck treats every argument passed to these functions as modified. As a result, the PR no longer reports constParameterReference or constVariableReference for read-only code such as return *std::begin(v);, std::begin(v) == std::end(v) or a read-only iterator loop. Base reports all of these. This fixes the std::begin(*p) false positive from C9, but it does so by making the functions mark their argument as changed every time, instead of checking how the returned iterator is used. Apart from this, I found no new false positives. The testrunner and test/cfg results are clean.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | FP for push_back(*p) into a container of std::reference_wrapper | AI review | lib/astutils.cpp:2797 |
🎫 Ticket #15103 |
| 2 | FP constParameterPointer when std::begin(*p)/std::end(*p) is used to write | AI review | lib/astutils.cpp:2797 |
✅ Fixed |
| 3 | inout on std::begin/end/rbegin/rend loses constParameterReference for read-only use | AI review | cfg/std.cfg:8838 |
🆕 New (false negative) |
@chrchr-github, please fix, resolve or reply to the open and new comments: #3.
For #3 (false negative), it is enough to create a Trac ticket and link it here. It does not need to be fixed in this PR.
Testing
Builds and tests
- I built
cppcheckandtestrunnerinpr/, andcppcheckinbase/. ./testrunner -q: 0 failed.- I ran
test/cfg/std.cppandstd.cwith the runtests.sh options, using both base and the PR. The output is identical, and no warnings are unmatched.
False negative found (scratch/b.cpp):
#include <vector>
#include <string>
#include <iterator>
int g1(std::vector<int>& v) { return *std::begin(v); }
bool g2(std::vector<int>& v) { return std::begin(v) == std::end(v); }
int g3(std::vector<int>& v) { return *std::rbegin(v); }
int g4(std::vector<int>& v) { return *(std::end(v) - 1); }
int g5(std::vector<int>& v) { int s = 0; for (auto it = std::begin(v); it != std::end(v); ++it) s += *it; return s; }
int g6() { std::vector<int> v(3); int* p = &v[0]; return *p + *std::begin(v); }
int g7(int* p) { std::vector<int>& v = *reinterpret_cast<std::vector<int>*>(p); return *std::begin(v); }Command: cppcheck -q --enable=style --inconclusive --library=std --template='{line}:{id} {message}' b.cpp
base:
4:constParameterReference Parameter 'v' can be declared as reference to const
5:constParameterReference Parameter 'v' can be declared as reference to const
6:constParameterReference Parameter 'v' can be declared as reference to const
7:constParameterReference Parameter 'v' can be declared as reference to const
8:constParameterReference Parameter 'v' can be declared as reference to const
10:constVariableReference Variable 'v' can be declared as reference to const
9:constVariablePointer Variable 'p' can be declared as pointer to const
8:useStlAlgorithm ...
PR:
9:constVariablePointer Variable 'p' can be declared as pointer to const
8:useStlAlgorithm ...
All six const-reference warnings are gone.
Other results
v.push_back(*p)(#15203) now givesconstParameterPointer, which is the expected warning.- The C9 cases (
std::fill(std::begin(*p), ...),*std::rbegin(*p) = 1) no longer give a warning. int a[3]; std::fill(std::begin(a), std::end(a), 0);gives no new uninitvar warning.int f(std::vector<int>* p){ return *std::begin(*p); }gives no warning in either base or the PR.
Automated review of d16a499 by Claude. The pull request was built and tested locally.
| <use-retval/> | ||
| <leak-ignore/> | ||
| <noreturn>false</noreturn> | ||
| <arg nr="1" direction="inout"/> |
There was a problem hiding this comment.
AI review
direction="inout" makes std::begin/std::rbegin (and the same change on std::end/std::rend) count their argument as modified every time, even when the iterator is only read. As a result, the PR loses warnings that base reports:
int g1(std::vector<int>& v) { return *std::begin(v); }
bool g2(std::vector<int>& v) { return std::begin(v) == std::end(v); }
int g3(std::vector<int>& v) { return *std::rbegin(v); }
int g5(std::vector<int>& v) { int s = 0; for (auto it = std::begin(v); it != std::end(v); ++it) s += *it; return s; }
int g7(int* p) { std::vector<int>& v = *reinterpret_cast<std::vector<int>*>(p); return *std::begin(v); }Base gives constParameterReference for g1–g5 and constVariableReference for g7. The PR gives none of these warnings. For a non-const argument, std::begin(x) behaves like the member x.begin(). Whether it changes anything depends on how the returned iterator is used, as it does for .begin(). So the C9 false positive would be better fixed in isVariableChanged(): when the stripped dereference goes into a function with a start-iterator/end-iterator yield, look at how the result is used. That would keep the library entry as in. Please also add a test that checks read-only std::begin(v) still gives constParameterReference.
|
Test results for commit d16a499 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9016/ 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. |
No description provided.