Skip to content

Fix #15226 FP constParameterPointer with unknown types or functions - #9083

Open
chrchr-github wants to merge 2 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_15226
Open

chrchr-github wants to merge 2 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_15226

Conversation

@chrchr-github

Copy link
Copy Markdown
Collaborator

No description provided.

@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 two new false negatives. Both changes are wider than ticket #15226 needs:

  1. lib/astutils.cpp: getTokenArgumentFunction() now also skips a +/- that has no valueType. Any arithmetic whose type is not known, for example g(h() + x) with an undeclared h, is now treated as passing x itself to g. For an unknown function g, isVariableChangedByFunctionCall() then assumes that x changes. Before the PR these warnings were reported and now they are not: knownConditionTrueFalse (also with an unknown type U), constParameterPointer and constVariablePointer.
  2. lib/checkother.cpp: with returnsReference(function, /*unknown*/ true), a plain auto return type and a lambda without a trailing return type count as a possible reference. Both always return by value, so constParameterPointer is 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.

Comment thread lib/astutils.cpp
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))

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

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 lost

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

Comment thread lib/checkother.cpp
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)))

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

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

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

@dmcppcheck

Copy link
Copy Markdown
Contributor

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/
packages: 100
Differing warnings: 25 (+2 -23)
AI review: 21 acceptable regression, 3 improvement, 1 unclear (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9083/2026-11-27_18-36-09-59fb540d47f0-review.txt)
Timing: +0.2% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9083/2026-11-27_18-36-09-59fb540d47f0_timing.html)

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.

@chrchr-github chrchr-github changed the title Fix #15226 Fix #15226 FP constParameterPointer with unknown types or functions Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants