Repository navigation
Fix #15226 FP constParameterPointer with unknown types or functions - #9083
chrchr-github wants to merge 7 commits into
Conversation
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found two new false negatives. Both changes are wider than ticket #15226 needs:
lib/astutils.cpp:getTokenArgumentFunction()now also skips a+/-that has no valueType. Any arithmetic whose type is not known, for exampleg(h() + x)with an undeclaredh, is now treated as passingxitself tog. For an unknown functiong,isVariableChangedByFunctionCall()then assumes thatxchanges. Before the PR these warnings were reported and now they are not:knownConditionTrueFalse(also with an unknown typeU),constParameterPointerandconstVariablePointer.lib/checkother.cpp: withreturnsReference(function, /*unknown*/ true), a plainautoreturn type and a lambda without a trailing return type count as a possible reference. Both always return by value, soconstParameterPointeris lost for them. The PR turns an existing passing test into a TODO because of this.
The new test only checks that the FP is gone. Nothing checks that warnings stay for unknown-typed +/- that is not a pointer.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Skipping +/- with unknown valueType makes x in g(h() + x) count as passed to g; warnings are lost | AI review | lib/astutils.cpp:2372 |
🆕 New (false negative) |
| 2 | unknown=true also treats auto/lambda returns as references, so constParameterPointer is lost for them | AI review | lib/checkother.cpp:2056 |
🆕 New (false negative) |
@chrchr-github, please fix, resolve or reply to the open and new comments: #1, #2.
For #1 and #2 (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
I built cppcheck and testrunner in pr/, and cppcheck in base/. Both built without errors. ./testrunner -q in pr/ ran 5427 tests and 0 failed.
Unknown-typed + (astutils change)
scratch/a.cpp:
void f1(int x0) {
int x = 0;
g(h() + x);
if (x == 0) {}
}
void f2() {
int x = 0;
g(1 + x);
if (x == 0) {}
}
void f3(int* p) {
g(h() + *p);
}
void f4(U x) {
U y = 0;
g(x + y);
if (y == 0) {}
}Command: cppcheck -q --enable=style,warning --inconclusive a.cpp
base:
a.cpp:17:11: style: Condition 'y==0' is always true [knownConditionTrueFalse]
a.cpp:4:11: style: The comparison 'x == 0' is always true. [knownConditionTrueFalse]
a.cpp:9:11: style: The comparison 'x == 0' is always true. [knownConditionTrueFalse]
a.cpp:11:14: style: Parameter 'p' can be declared as pointer to const [constParameterPointer]
PR: only a.cpp:9:11 ... [knownConditionTrueFalse] is left. The warnings for f1, f3 and f4 are lost.
scratch/b.cpp:
void f2(int* p) { int* q = nullptr; g(h() + *q); }
void f4(int* p) { int* q = p; g(h() - *q); }base reports constVariablePointer for q in both functions. The PR reports neither of them. nullPointer and uninitvar are the same in base and PR.
Unknown return type (checkother change)
scratch/c.cpp:
auto f1(int* p) { return *p; }
auto f2(int* p) -> int { return *p; }
int f3() { int i = 0; return [](int* p) { return *p; }(&i); }
U f4(int* p) { return *p; }
int f5(int* p) { return *p; }base reports constParameterPointer for f1, f2, f3, f4 and f5. The PR reports it only for f2 and f5. Dropping f4 (U may be a reference typedef) is what the ticket asks for. Dropping f1 and the lambda in f3 is a regression, because they return by value.
Automated review of 59fb540 by Claude. The pull request was built and tested locally.
| while (parent && parent->isCast()) | ||
| parent = parent->astParent(); | ||
| if (Token::Match(parent, "[+-]") && parent->valueType() && parent->valueType()->pointer) | ||
| if (Token::Match(parent, "[+-]") && (!parent->valueType() || parent->valueType()->pointer)) |
There was a problem hiding this comment.
AI review
With this change, a +/- whose valueType is unknown is skipped too. That covers any operand of unknown type: an undeclared function call, or a variable of an unknown type U. In g(h() + x), getTokenArgumentFunction(x) now returns g. Because g is unknown, isVariableChangedByFunctionCall() falls through to "Safe guess: Assume that parameter is changed", although only a temporary is passed.
Reproduced regressions, all reported by base and no longer by the PR:
void f1() { int x = 0; g(h() + x); if (x == 0) {} } // knownConditionTrueFalse lost
void f3(int* p) { g(h() + *p); } // constParameterPointer lost
void f4(U x) { U y = 0; g(x + y); if (y == 0) {} } // knownConditionTrueFalse lost
void f5(int* p) { int* q = p; g(h() - *q); } // constVariablePointer lostIt would be safer to skip the operator only when one of its operands is (or may be) a pointer, for example when the operand tok itself has a pointer valueType. Alternatively, fix the #15226 case in checkConstPointer alone instead of in this shared helper, which has more than 15 callers.
|
Test results for commit 59fb540 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9083/ 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 +/- change in lib/astutils.cpp (C2) has not changed and still causes false negatives: an operand of unknown type next to +/- is still treated as if it were passed to the enclosing unknown function. The checkReturns/returnsReference rework now fixes C3. Plain auto return types and lambdas without a trailing return type count as non-reference again, so constParameterPointer is reported for them. It is also no longer reported in one case where it was a false positive: a lambda with -> decltype(auto). I found no new problems.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Skipping +/- with unknown valueType in getTokenArgumentFunction loses warnings (g(h() + x)) | AI review | lib/astutils.cpp:2372 |
|
| 2 | returnsReference(unknown=true) treats auto/lambda returns as references, losing constParameterPointer | AI review | lib/checkother.cpp:2056 |
✅ Fixed |
@chrchr-github, please fix, resolve or reply to the open and new comments: #1.
For #1 (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
- I built
cppcheckandtestrunnerin pr/ andcppcheckin base/ without errors../testrunner -qran 5427 tests and 0 failed. - Return types (
scratch/a.cpp, checked withcppcheck -q --enable=style --inconclusive). I tried trailing-> int&and-> int,decltype(auto),auto&,const auto, an unknownUreturn type, lambdas with and without a trailing return type, and out-of-classauto S::gandU S::h.- Matches between base and PR:
auto,const auto, a lambda without a trailing return type,-> intandauto S::gare still reported in the PR. - Dropped by the PR, as the ticket intends:
U f(int*),U S::hand-> U. - Also dropped by the PR:
[](int* p) -> decltype(auto) { return (*p); }. That lambda returnsint&, so base's warning was a false positive. - Not reported in either:
-> int&,decltype(auto)andauto&.
- Matches between base and PR:
- C2 is still reproducible. Test file:
base reports
void f3(int* p) { g(h() + *p); } void f1() { int x = 0; g(h() + x); if (x == 0) {} }
constParameterPointerandknownConditionTrueFalse. The PR reports neither.
Automated review of 13b50f4 by Claude. The pull request was built and tested locally.
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The new astIsPointer(tok) condition in getTokenArgumentFunction() causes a new false negative. When a pointer operand sits under +/- and the result is known not to be a pointer, as in a pointer difference g(p - q) (ptrdiff_t), the operator is now skipped anyway. Both pointers then count as passed to the unknown g and as possibly changed. The PR loses constParameterPointer, constVariablePointer and knownConditionTrueFalse warnings that base reports. The earlier comments are fixed: unknown-typed +/- (C2) and auto/lambda return types (C3). I found no other new problems.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Skipping +/- with unknown valueType loses warnings for g(h() + x) | AI review | lib/astutils.cpp:2372 |
✅ Fixed |
| 2 | returnsReference(unknown=true) treats auto/lambda returns as references | AI review | lib/checkother.cpp:2056 |
✅ Fixed |
| 3 | astIsPointer(tok) also skips pointer differences like g(p - q); p and q then count as possibly changed | AI review | lib/astutils.cpp:2372 |
🆕 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
- I built
cppcheckandtestrunnerin pr/ andcppcheckin base/ without errors../testrunner -qpasses with 0 failures. - The C2 cases (
g(h() + *p),g(h() - *q)) and the C3 case (auto h1(int* p)) are covered by the new test, and they are reported again. - For lambdas, I checked the
returnsPointer,returnsVoidandreturnLocalStdMovecallers ofcheckReturns. I found no differences from base in my samples.
Pointer difference (new FN)
scratch/a.cpp:
void f1(int* p, int* q) { g(p - q); }
void f2(int* p, int* q) { int d = 0; g(p - q, d); }
struct S { void m(int); };
void f8(S& s, int* p, int* q) { s.m(p - q); }scratch/c.cpp:
void f(int* p, int* q) {
int* a = p;
int* b = q;
g(a - b);
}
void k(int* p, int* q) {
int* a = nullptr;
g(a - q);
if (a == nullptr) {}
}Command: cppcheck -q --enable=style,warning --inconclusive a.cpp c.cpp
-
For a.cpp, base reports
constParameterPointerforpandqin f1 and f2. The PR reports neither. f8, where the callee is known, is reported by both. -
For c.cpp, base reports:
knownConditionTrueFalsefora==nullptr(9:11)constVariablePointerfora/b(2:10, 3:10, 7:10)constParameterPointerforp/qink
The PR reports only
nullPointerArithmetic.
Automated review of 4f770a2 by Claude. The pull request was built and tested locally.
| while (parent && parent->isCast()) | ||
| parent = parent->astParent(); | ||
| if (Token::Match(parent, "[+-]") && parent->valueType() && parent->valueType()->pointer) | ||
| if (Token::Match(parent, "[+-]") && (astIsPointer(parent) || astIsPointer(tok))) |
There was a problem hiding this comment.
AI review
astIsPointer(tok) is true for each operand of a pointer difference. The result of p - q is a known non-pointer type (ptrdiff_t), yet the - is now skipped, and getTokenArgumentFunction(p) returns g. If g is unknown, p and q are assumed to be changed.
Reproduced. Base reports these and the PR does not:
void f1(int* p, int* q) { g(p - q); } // constParameterPointer p, q lost
void f(int* p, int* q) { int* a = p; int* b = q; g(a - b); } // constVariablePointer a, b lost
void k(int* q) { int* a = nullptr; g(a - q); if (a == nullptr) {} } // knownConditionTrueFalse lostSuggestion: fall back to astIsPointer(tok) only when parent has no valueType, for example (astIsPointer(parent) || (!parent->valueType() && astIsPointer(tok))).
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no new problems. The pointer-difference false negative from C7 is fixed: g(p - q) reports constParameterPointer for p and q again, and the new test covers it. The C2 and C3 fixes are still in place. The return-type rework drops the warning for unknown return types, as the ticket asks. The auto, constexpr auto, -> auto and lambda cases are still reported.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Skipping +/- with unknown valueType loses warnings for g(h() + x) | AI review | lib/astutils.cpp:2372 |
✅ Fixed |
| 2 | returnsReference(unknown=true) treats auto/lambda returns as references | AI review | lib/checkother.cpp:2056 |
✅ Fixed |
| 3 | astIsPointer(tok) also skips pointer differences like g(p - q) | AI review | lib/astutils.cpp:2372 |
✅ Fixed |
Testing
- I built
cppcheckandtestrunnerin pr/ andcppcheckin base/. Both built without errors../testrunner -qran 5427 tests and 0 failed, including the new #15226 case in TestOther. - Pointer arithmetic under an unknown function. Test cases:
g(p - q)withU* q,g(p - a)with an array parameter and a local array,g((char*)p - (char*)q),g(p - &*q),g(p - v.data()),g(p - q + 1)andg(q + (p - q)).- All of them give the same warnings in base and PR.
- The only difference is
void f2(int* p, int* q) { g(p - h(q)); }with an undeclaredh: base reportsconstParameterPointerforpand the PR does not. Without knowing whathreturns,p - h(q)may be a pointer, so treatingpas possibly passed on is a defensible conservative choice. I do not count it as a problem.
- Return types (
--enable=style --inconclusive --library=std):- Reported by both base and PR:
auto S::m(int*)(out of class),constexpr auto, the lambda-> auto, and a templateauto. - Dropped by the PR, as the ticket intends:
U S::n,U::typeandstd::vector<int>::reference. The last two were false positives in base. - Not reported by either: the lambda
-> auto&.
- Reported by both base and PR:
Automated review of ea544a4 by Claude. The pull request was built and tested locally.
No description provided.