2

Following is the minimum working example (ok, in fact it's minimum non-working example :-)). When compiled with gcc (from version 5.0 up to 9.3) it fires the following warning. It even seems to fire the warning only in release build (-02 and higher).

Code:

class A
{
};

class B
{
    const A& getA() const
    {
        static A a;
        return a;
    }
    const A& get(bool b) const; 
};

const A& B::get(bool b) const
{
    return static_cast<const A&>(b ? getA() : getA());
}

int main(int argc, char** argv)
{
    return 0;
}

Compiler Output:

<source>: In member function 'const A& B::get(bool) const':
<source>:17:50: warning: function returns address of local variable [-Wreturn-local-addr]
  return static_cast<const A&>(b ? getA() : getA());
<source>:17:50: note: declared here
  return static_cast<const A&>(b ? getA() : getA());
Compiler returned: 0

The code above compiles ok with both MSVC and clang and it even compiles fine with gcc 10.1. It also compiles ok in debug (with -O1 or -O0) with older gcc versions.

Can you see anything incorrect in the code, or is it really a compiler issue?

Observation

When I add a deleted copy constructor to class A (A(const A&) = delete;), the warning disappears and the compiler stops to create a local variable.

Try You can try on gcc.godbolt.org

Mi-La
  • 685
  • 10
  • 18
  • Why using gcc for c++? – David G. Pickett Jun 11 '20 at 16:07
  • @DavidG.Pickett Why not? – eerorika Jun 11 '20 at 16:07
  • Looks like this is false positive warning. And it looks like it has already been fixed in gcc 10. – eerorika Jun 11 '20 at 16:08
  • 1
    @eerorika It's not a false positive, if you look at the generated code, it's incorrect. Even with `-O0` the generated code returns an invalid reference. – IlCapitano Jun 11 '20 at 16:12
  • 1
    If you remove the `static_cast` it seems to have the expected behaviour. – IlCapitano Jun 11 '20 at 16:17
  • @IlCapitano If generated code is wrong too, then it is a much worse bug in the compiler than merely broken warning. – eerorika Jun 11 '20 at 16:19
  • @IlCapitano Correct. In fact it is how we fixed our real issue. The problem ocurred in generated code and it is not clear why the `static_cast` should cause any problem :/. – Mi-La Jun 11 '20 at 16:24
  • It is fixed in GCC10. If you look at how GCC is developped, with bug fixes first introduced in the master branch, and then sometime cherry picked to release branch... I suppose it is more conservative to use the very last GCC version. – Oliv Jun 11 '20 at 16:27
  • @Oliv So was it really a gcc bug? I just wanted to be sure that the code is correct. Are you able to find the proper bug in the gcc bugzilla. The bugzilla seems to be much more buggy than the gcc (at least its search), so I'm not able to find anything there :-). – Mi-La Jun 11 '20 at 17:33
  • So far I've found this: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67795. Seems to be related issue. It's also interesting that it occurs only with static_cast. When you use reinterpret_cast, const_cast or c-style cast, it works correctly. I thought that c-style cast should perform const_cast or static_cast C++??? – Mi-La Jun 11 '20 at 19:50
  • Yes a c-style cast, is equivalent, to a static_cast if the static_cast is not ill-formed. The fact GCC does not produce the same code with the c-style cast is an other proof that GCC was bugged. – Oliv Jun 11 '20 at 20:34

0 Answers0