https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121840
Bug ID: 121840
Summary: Bogus -Wuninitialized with virtual inheritance inside
constructor & -O2
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: romain.geissler at amadeus dot com
Target Milestone: ---
Hi,
This piece of code will emit a bogus -Wuninitialized when compiled with "-O2
-Wall":
<<END_OF_REPRODUCER
//#include <iostream>
struct VirtualBase
{
VirtualBase(int* intPointer)
:_intPointer(intPointer)
{
//std::cout << __PRETTY_FUNCTION__ << std::endl;
}
int* _intPointer;
};
int SomeGlobalInt;
void f(int& i)
{
SomeGlobalInt = i;
//std::cout << __PRETTY_FUNCTION__ << ": " << i << std::endl;
}
struct Derived : public virtual VirtualBase
{
Derived();
virtual void AbstractMethod() = 0;
};
Derived::Derived()
:VirtualBase(new int{42}) // This constructor call is ignored as expected,
as it is not the most derived class
{
//std::cout << __PRETTY_FUNCTION__ << std::endl;
f(*_intPointer); // bogus warning here, complaining _intPointer is not
initialized, while normally it is
}
struct Final : public Derived
{
Final()
:VirtualBase(new int{53}),
Derived()
{
//std::cout << __PRETTY_FUNCTION__ << std::endl;
}
void AbstractMethod() override {};
};
int main()
{
Final final;
return SomeGlobalInt;
}
END_OF_REPRODUCER
The generated warning is this one:
<source>: In constructor 'Derived::Derived()':
<source>:34:8: warning: '((VirtualBase*)this)[1].VirtualBase::_intPointer' is
used uninitialized [-Wuninitialized]
34 | f(*_intPointer); // bogus warning here, complaining _intPointer is
not initialized, while normally it is
| ^~~~~~~~~~~
No warning was generated with gcc <= 13, it seems it started with gcc >= 14.
Current trunk is still affected (see on Compiler Explorer here:
https://godbolt.org/z/o68azGGPW)
Clang doesn't produce anything like that, and anyway, the execution of the
program really yield an exit code of "53" so in practice "_intPointer" is
actually well initiliazed to 53 as expected where the warning is emitted, so
that warning seems definitely wrong.