Repository navigation
Fix #15226 - #9083
Fix #15226#9083chrchr-github wants to merge 2 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.
| if (Token::simpleMatch(gparent, "return")) { | ||
| const Function* function = getEnclosingFunction(var); | ||
| if (function && (!Function::returnsReference(function) || Function::returnsConst(function))) | ||
| if (function && (!Function::returnsReference(function, /*unknown*/ true) || Function::returnsConst(function))) |
There was a problem hiding this comment.
AI review
returnsReference(function, true) also returns true when the return type can not be determined. That includes a plain auto return type and lambdas without a trailing return type. Both always deduce a non-reference type, but they now count as possibly returning a reference:
auto f1(int* p) { return *p; } // constParameterPointer lost (base reports it)
int f3() { int i = 0; return [](int* p) { return *p; }(&i); } // lost, see the new TODO in testother.cppThe ticket case U g(int* p, int n) could be handled without losing these, for example by treating only an unknown named return type as a possible reference and not auto or a missing return type.
|
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. |
No description provided.