https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95825

daniel.klauer at gin dot de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |daniel.klauer at gin dot de

--- Comment #7 from daniel.klauer at gin dot de ---
Reduced test case:


template<typename T>
struct tc_optional_base
{
        // default ctor leaves m_storage uninitialized
        tc_optional_base() : m_initialized(false) {}
        bool m_initialized;
        T m_storage;
};

template<typename T>
tc_optional_base<T> f()
{
        return {};
}

tc_optional_base<int> g()
{
        return f<int>();
}


$ g++ -Wall -O1 -fsanitize=address b.cxx -c
In function ‘tc_optional_base<T> f() [with T = int]’,
    inlined from ‘tc_optional_base<int> g()’ at b.cxx:18:15:
b.cxx:13:17: warning: ‘<anonymous>.tc_optional_base<int>::m_storage’ is used
uninitialized [-Wuninitialized]
   13 |         return {};
      |                 ^
b.cxx: In function ‘tc_optional_base<int> g()’:
b.cxx:13:17: note: ‘<anonymous>’ declared here
   13 |         return {};
      |

$ g++ --version
g++ (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0


It looks like the m_storage field really is uninitialized, although I think in
practice boost::optional does not access it in that state in its implementation
thanks to m_initialized. Does gcc warn about the uninitialized data during
copying of the object here? It does seem to be correct...

In the above case with templates it happens at -O1 already, but without
templates it happens too, at -O2.

Reply via email to