From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id B1C463858D28; Tue, 14 Dec 2021 15:45:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B1C463858D28 From: "pierre.mantion at cern dot ch" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/103711] New: Virtual base destroyed twice when an exception is thrown in a derived class' constructor called from a delegated constructor Date: Tue, 14 Dec 2021 15:45:48 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 11.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: pierre.mantion at cern dot ch X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 Dec 2021 15:45:48 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D103711 Bug ID: 103711 Summary: Virtual base destroyed twice when an exception is thrown in a derived class' constructor called from a delegated constructor Product: gcc Version: 11.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: pierre.mantion at cern dot ch Target Milestone: --- Created attachment 52000 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D52000&action=3Dedit preprocessed file that triggers the bug Virtual base destroyed twice when an exception is thrown in a derived class' constructor called from a delegated constructor: The code below causes A=E2=80=99s destructor to be called twice on the same= object. This is the most I could reduce the code to have it trigger the bug. #include int constructions =3D 0; int destructions =3D 0; struct A { A() { constructions++; } virtual ~A() { destructions++; } }; struct B : public virtual A { B(int) { }; B() : B(1) { throw -1; } virtual ~B() =3D default; }; struct C : public B { }; int main() { try { C c; } catch (int e) { std::cout << "Caught: " << e << std::endl; } std::cout << constructions << " constructions" << std::endl; std::cout << destructions << " destructions" << std::endl; return 0; } Expected output: Caught: -1 1 constructions 1 destructions Actual output: Caught: -1 1 constructions 2 destructions This seems to be a combination of the =E2=80=9Cvirtual=E2=80=9D inheritance= in B, the call to the delegating constructor in B, and the exception thrown in B=E2=80=99s no= arg constructor. Removing either of these makes the program behave as expected. Constructing a B instead of a C works fine. GCC latest version and ICC latest version have the same issue, but clang la= test version doesn=E2=80=99t. Tested on Godbolt: - Works/behaves as expected with Clang 13 (https://godbolt.org/z/4Yr5hWW5c) - fails with GCC 11.2 (https://godbolt.org/z/cr8bz4hMc) - fails with MSVC 19.29 (https://godbolt.org/z/b3Pd5PEfe) - fails with ICC 2021.3.0 (https://godbolt.org/z/caM41fjao) - (other compilers not tested yet). The problematic behavior was reproduced locally with version of GCC: % gcc -v Using built-in specs. COLLECT_GCC=3Dgcc COLLECT_LTO_WRAPPER=3D/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /build/gcc/src/gcc/configure --prefix=3D/usr --libdir=3D/u= sr/lib --libexecdir=3D/usr/lib --mandir=3D/usr/share/man --infodir=3D/usr/share/in= fo --with-bugurl=3Dhttps://bugs.archlinux.org/ --enable-languages=3Dc,c++,ada,fortran,go,lto,objc,obj-c++,d --with-isl --with-linker-hash-style=3Dgnu --with-system-zlib --enable-__cxa_atexit --enable-cet=3Dauto --enable-checking=3Drelease --enable-clocale=3Dgnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-install-libiberty --enable-linker-build= -id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=3Dposix --disable-libssp --disable-libstdcxx-pch --disable-libunwind-exceptions --disable-werror gdc_include_dir=3D/usr/include/dlang/gdc Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.1.0 (GCC)=20 The incorrect behavior can be reproduced as well with GCC 4.8.5 (need to pa= ss -std=3Dc++11 to compile in that case). % gcc -v Using built-in specs. COLLECT_GCC=3D/acc/sys/L867/usr/bin/g++ COLLECT_LTO_WRAPPER=3D/nfs/cs-ccr-felab/sys/L867/usr/bin/../libexec/gcc/x86= _64-redhat-linux/4.8.5/lto-wrapper Target: x86_64-redhat-linux Configured with: ../configure --prefix=3D/usr --mandir=3D/usr/share/man --infodir=3D/usr/share/info --with-bugurl=3Dhttp://bugzilla.redhat.com/bugz= illa --enable-bootstrap --enable-shared --enable-threads=3Dposix --enable-checking=3Drelease --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=3Dgnu --enable-languages=3Dc,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-pl= ugin --enable-initfini-array --disable-libgcj --with-isl=3D/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-lin= ux/isl-install --with-cloog=3D/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-l= inux/cloog-install --enable-gnu-indirect-function --with-tune=3Dgeneric --with-arch_32=3Dx86-64 --build=3Dx86_64-redhat-linux Thread model: posix gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)=20 Complete command to reproduce (GCC 11.1.0): g++ -v -save-temps -Wall -Wextra reproducer.cpp && ./a.out Complete command to reproduce (GCC 4.8.5): g++ -v -save-temps -Wall -Wextra reproducer.cpp -std=3Dc++11 && ./a.out=