From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 577823839828 for ; Thu, 22 Jul 2021 10:04:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 577823839828 Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-569-uR9jtoDuMwmLsf7jAI9gtA-1; Thu, 22 Jul 2021 06:04:02 -0400 X-MC-Unique: uR9jtoDuMwmLsf7jAI9gtA-1 Received: by mail-wm1-f69.google.com with SMTP id j11-20020a05600c410bb02902278758ab90so505233wmi.9 for ; Thu, 22 Jul 2021 03:04:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=N53zh8mu/h6rdNAJ8jFTRuip6rakzqFQPX5xiXdFMWo=; b=Ywdn7VawG525fAE2ADAorbXUMC+iIInsb+GMxAQTnAtDHQiqyov24B7R97CgY8kyKt bHEri9VoD8vm0b6lsaaeEbLAfNwE2WDm40RMk8FqTJvpbS474ABxlfXntH5uicyPJFBe 5XAdE1mUru7VYOF/2lPmDaoUXZhri4QONjrwv1YCajB/St7T4wPWa6OufQ2enNulfG7b vVGLcNa1iWN+mrnHKQmQcoLSZVJe9gO4YJsSQQx3w02bLRg8RzmyMO1v+zdzqpXPZseA P+5X4XAeIcKuCpeWtOHBXiWZOsTlyHRqBxw+o7vC3Sj9bZaIKr2DaqtZhe3TDf30JvyH 55OQ== X-Gm-Message-State: AOAM532uTlOKlVnxBqs9YXKh9Zc4w7wy6q0noRzm//Iq7ivPnxm1W7QU 6fKrhyRnga9P+dO72fFCvlsvhedW+YQzTTRmdwNoiEn8yGzGiz15QcvVrGM+l6rCQDnAINffWI/ XSQB2jO6w9+NRuCE1ijRNy0Wc4vQjVt4= X-Received: by 2002:adf:edc8:: with SMTP id v8mr46288300wro.279.1626948240964; Thu, 22 Jul 2021 03:04:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwm3HZnY5ZDXl4oHfJzu0gQbSM1UMaV+9pcwoEN81z/P6s4tyYRPV/qBTQXLmAywfPG2jqTCCp/J7s5IB8lxJg= X-Received: by 2002:adf:edc8:: with SMTP id v8mr46288280wro.279.1626948240741; Thu, 22 Jul 2021 03:04:00 -0700 (PDT) MIME-Version: 1.0 References: <2a9e7039-b23b-b0d6-c34f-56a1957df591@redhat.com> In-Reply-To: <2a9e7039-b23b-b0d6-c34f-56a1957df591@redhat.com> From: Jonathan Wakely Date: Thu, 22 Jul 2021 11:03:49 +0100 Message-ID: Subject: Re: More aggressive GCC 12 -Wmaybe-uninitialized when using To: Stephan Bergmann Cc: gcc Mailing List , "libstdc++" X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 22 Jul 2021 10:04:09 -0000 On Thu, 22 Jul 2021 at 10:23, Stephan Bergmann via Libstdc++ wrote: > > Compared to GCC 11 (at least gcc-c++-11.1.1-3.fc34.x86_64), recent GCC > 12 trunk emits two "unhelpful" -Wmaybe-uninitialized for > > > $ cat test.cc > > #include > > using fn =3D std::function; > > fn f(fn x) { > > fn a; > > a =3D x; > > return x; > > } > > > $ ~/gcc/trunk/inst/bin/g++ -c -Wmaybe-uninitialized -O2 test.cc > > In file included from ~/gcc/trunk/inst/include/c++/12.0.0/bits/stl_func= tion.h:60, > > from ~/gcc/trunk/inst/include/c++/12.0.0/functional:49= , > > from test.cc:1: > > In function =E2=80=98std::_Require >, std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp> > std::s= wap(_Tp&, _Tp&) [with _Tp =3D void (*)(const std::_Any_data&)]=E2=80=99, > > inlined from =E2=80=98void std::function<_Res(_ArgTypes ...)>::swap= (std::function<_Res(_ArgTypes ...)>&) [with _Res =3D void; _ArgTypes =3D {}= ]=E2=80=99 at ~/gcc/trunk/inst/include/c++/12.0.0/bits/std_function.h:529:1= 1, > > inlined from =E2=80=98std::function<_Res(_ArgTypes ...)>& std::func= tion<_Res(_ArgTypes ...)>::operator=3D(const std::function<_Res(_ArgTypes .= ..)>&) [with _Res =3D void; _ArgTypes =3D {}]=E2=80=99 at ~/gcc/trunk/inst/= include/c++/12.0.0/bits/std_function.h:442:20, > > inlined from =E2=80=98fn f(fn)=E2=80=99 at test.cc:5:9: > > ~/gcc/trunk/inst/include/c++/12.0.0/bits/move.h:204:11: warning: =E2=80= =98.std::function::_M_invoker=E2=80=99 may be used uniniti= alized [-Wmaybe-uninitialized] > > 204 | _Tp __tmp =3D _GLIBCXX_MOVE(__a); > > | ^~~~~ > > In file included from ~/gcc/trunk/inst/include/c++/12.0.0/functional:59= , > > from test.cc:1: > > ~/gcc/trunk/inst/include/c++/12.0.0/bits/std_function.h: In function = =E2=80=98fn f(fn)=E2=80=99: > > ~/gcc/trunk/inst/include/c++/12.0.0/bits/std_function.h:442:9: note: = =E2=80=98=E2=80=99 declared here > > 442 | function(__x).swap(*this); > > | ^~~~~~~~~~~~~ > > In file included from ~/gcc/trunk/inst/include/c++/12.0.0/bits/stl_func= tion.h:60, > > from ~/gcc/trunk/inst/include/c++/12.0.0/functional:49= , > > from test.cc:1: > > In function =E2=80=98std::_Require >, std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp> > std::s= wap(_Tp&, _Tp&) [with _Tp =3D std::_Any_data]=E2=80=99, > > inlined from =E2=80=98void std::function<_Res(_ArgTypes ...)>::swap= (std::function<_Res(_ArgTypes ...)>&) [with _Res =3D void; _ArgTypes =3D {}= ]=E2=80=99 at ~/gcc/trunk/inst/include/c++/12.0.0/bits/std_function.h:527:1= 1, > > inlined from =E2=80=98std::function<_Res(_ArgTypes ...)>& std::func= tion<_Res(_ArgTypes ...)>::operator=3D(const std::function<_Res(_ArgTypes .= ..)>&) [with _Res =3D void; _ArgTypes =3D {}]=E2=80=99 at ~/gcc/trunk/inst/= include/c++/12.0.0/bits/std_function.h:442:20, > > inlined from =E2=80=98fn f(fn)=E2=80=99 at test.cc:5:9: > > ~/gcc/trunk/inst/include/c++/12.0.0/bits/move.h:204:11: warning: =E2=80= =98*(std::_Any_data*)((char*)& + offsetof(std::function, std::func= tion::))=E2=80=99 may be used uninitialized [-Wmaybe-unini= tialized] > > 204 | _Tp __tmp =3D _GLIBCXX_MOVE(__a); > > | ^~~~~ > > In file included from ~/gcc/trunk/inst/include/c++/12.0.0/functional:59= , > > from test.cc:1: > > ~/gcc/trunk/inst/include/c++/12.0.0/bits/std_function.h: In function = =E2=80=98fn f(fn)=E2=80=99: > > ~/gcc/trunk/inst/include/c++/12.0.0/bits/std_function.h:442:9: note: = =E2=80=98=E2=80=99 declared here > > 442 | function(__x).swap(*this); > > | ^~~~~~~~~~~~~ > > This appears to be an issue with more aggressive -Wmaybe-uninitialized > in GCC 12 vs. 11, rather than an issue with changes to in > libstdc++ 12 vs. 11, as effectively the same warnings are emitted when I > use GCC 12 with libstdc++ 11 with > > > $ ~/gcc/trunk/inst/bin/g++ -c -Wmaybe-uninitialized -O2 -nostdinc++ -is= ystem /usr/include/c++/11 -isystem /usr/include/c++/11/x86_64-redhat-linux = test.cc > > The warnings may technically be correct, and I'm not sure whether this > is something that should be addressed in the GCC code emitting the > warnings or in the libstdc++ implementation. > > (I found this when building LibreOffice with recent GCC 12 trunk.) The problem is that the _Function_base default constructor is user-provided, so when std::function value-initializes its base class, that doesn't do zero-init first. It just calls the default ctor, which doesn't initialize the _M_fucntor member. This should fix it: --- a/libstdc++-v3/include/bits/std_function.h +++ b/libstdc++-v3/include/bits/std_function.h @@ -237,7 +237,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { __functor._M_access<_Functor*>() =3D new _Functor(std::move(__f));= } }; - _Function_base() : _M_manager(nullptr) { } + _Function_base() =3D default; ~_Function_base() { @@ -250,8 +250,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION typedef bool (*_Manager_type)(_Any_data&, const _Any_data&, _Manager_operation); - _Any_data _M_functor; - _Manager_type _M_manager; + _Any_data _M_functor{}; + _Manager_type _M_manager{}; }; template @@ -634,8 +634,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION private: using _Invoker_type =3D _Res (*)(const _Any_data&, _ArgTypes&&...); - _Invoker_type _M_invoker; - }; + _Invoker_type _M_invoker =3D nullptr; + }; #if __cpp_deduction_guides >=3D 201606 template