Skip to content

Partial fix for #15291 FN returnReference with cast - #9094

Open
chrchr-github wants to merge 3 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_15291
Open

chrchr-github wants to merge 3 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_15291

Conversation

@chrchr-github

Copy link
Copy Markdown
Collaborator

No description provided.

Comment thread lib/checkautovariables.cpp Fixed

@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

The new loop removes every cast, including casts to a value type such as (int)i, static_cast<int>(i) and (const A)a. Those casts create a prvalue temporary, so a function returning const int& now gets returnReference ("Reference to local variable returned") instead of the right diagnosis, which is a reference to a temporary. Base reports nothing for these cases, so the code is still buggy, but the message names the wrong cause. The reference-cast case that the PR targets works for C-style, static_cast, reinterpret_cast and const_cast, for nested casts and for templates, with no false positives for references to globals, statics or references. Apart from this I found no new problems.

Comments

# Comment By Where Status
1 Redundant condition 'varTok' or possible null pointer dereference @github-advanced-security[bot] lib/checkautovariables.cpp:584 ✅ Fixed
2 Value casts are stripped too, so a returned temporary is reported as a reference to a local variable AI review lib/checkautovariables.cpp:584 🆕 New

@chrchr-github, please fix, resolve or reply to the open and new comments: #2.

Testing

I built cppcheck and testrunner in pr/ and cppcheck in base/. ./testrunner TestAutoVariables passed: 109 tests, 0 failures, 4 todos.

Reference casts. I ran pr/cppcheck -q --enable=all --inconclusive a.cpp on these:

  • (int&)i, static_cast<int&>(i), reinterpret_cast<int&>(i), const_cast<int&>(i), (long&)i, (int&)(i), (const int&)(int&)i and (T&)t in a template are now reported as returnReference.
  • (int&)g for a global, (int&)x for a static, (int&)r for a reference parameter and (int&)*p are not reported, which is correct.
  • (int&)s.m and (int&)(i, i) are still not reported. Neither base nor PR reports them.

Value casts. These now get the wrong message. Test file b.cpp:

const int& f1(int i) { return (int)i; }
const long& f2(int i) { return (long)i; }
const int& f3(int i) { return static_cast<int>(i); }
const int& f4(int i) { return int(i); }
struct A { int x; };
const A& f5(A a) { return (const A)a; }

Command: cppcheck -q --enable=warning,style --inconclusive b.cpp

PR output:

b.cpp:1:31: error: Reference to local variable returned. [returnReference]
b.cpp:2:32: error: Reference to local variable returned. [returnReference]
b.cpp:3:47: error: Reference to local variable returned. [returnReference]
b.cpp:4:34: error: Reference to temporary returned. [returnTempReference]
b.cpp:6:27: error: Reference to local variable returned. [returnReference]

Base reports only line 4, as returnTempReference. Lines 1, 2, 3 and 6 return a reference to a temporary made by the cast, not to the local i or a. The functional cast on line 4 is classified correctly, so the cast forms are now reported inconsistently.

Rvalue-reference cast. const int& f5(int i) { return (int&&)i; } is now reported as returnReference; base reports nothing.

There were no crashes on the edge cases I tried.


Automated review of 85101ba by Claude. The pull request was built and tested locally.

Comment thread lib/checkautovariables.cpp Outdated
continue;
const Variable* var = lt.token->variable();
const Token* varTok = lt.token;
while (varTok->isCast())

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

This loop removes every cast, not only casts to a reference type. A value cast like return (int)i;, return static_cast<int>(i); or return (const A)a; in a function returning const int& / const A& makes a temporary. Since the cast was removed, the code ends up at the local i and reports returnReference ("Reference to local variable returned"). The real bug is a reference to a temporary, and the functional cast int(i) in the same position is still reported as returnTempReference. You could remove only casts whose result is a reference, for example by checking that varTok->valueType() has reference != Reference::None, and leave value casts to the temporary-reference handling.

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.

4 participants