From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id A29D83858CDA; Mon, 5 Feb 2024 17:01:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A29D83858CDA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1707152460; bh=mNyKjRT7KbQ+vBwlnuz2ysT91OgTKZmO9JY/oP48t4w=; h=From:To:Subject:Date:From; b=JvfmDehv+WhQcbE0qJ+ec2y8QOTmfezW9BcSXRll++yjjFGrsp6S5aFlnyXRMXglo qon87DLvMJnE+G+sAdVq7tw46XGGPaeRekbR75KvhbMX8BNx1PgF11wZT0q/h/Yo2h J60RBLCxRdCB5JwMfjEAWFL814kaxEY+X5+nGCiE= From: "znhihgiasy at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/113773] New: coroutines: promise deconstructed twice if throwing from return object Date: Mon, 05 Feb 2024 17:01:00 +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: 12.3.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: znhihgiasy at gmail dot com 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 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D113773 Bug ID: 113773 Summary: coroutines: promise deconstructed twice if throwing from return object Product: gcc Version: 12.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: znhihgiasy at gmail dot com Target Milestone: --- The bug is about C++ coroutines. When I throw exceptions inside the return object's type conversion operator, the promise (and maybe the coroutine sta= te) is deconstructed twice. The C++ standard is not clear about when the conver= sion operator is called, and what happens when it throws. However clang's behavi= or is error-free and intuitive to me: The promise is deconstructed after continuing from the final suspend point and the exception is propagated to = the call site of resume(). Here is a minimum example: #include #include #include struct result { operator int() { throw 42; } }; class promise { public: result get_return_object() { return {}; } std::suspend_never initial_suspend() { std::cout << "initial suspend" << std::endl; return {}; } void unhandled_exception() { std::cout << "unhandled exception" << std::e= ndl; } std::suspend_never final_suspend() noexcept { std::cout << "final suspend= " << std::endl; return {}; } void return_void() {} ~promise() { std::cout << "~promise()" << std::endl; } }; template struct std::coroutine_traits { using promise_type =3D promise; }; int f() { co_return; } int main() { try { f(); } catch (int i) { assert(i =3D=3D 42); std::cout << "caught 42" << std::endl; } return 0; } Compiling with `g++ --std=3Dc++20 -g -O0 -Wall -Wextra gccbug.cpp` produced= this output: initial suspend final suspend ~promise() ~promise() free(): double free detected in tcache 2 I am running G++ version 12.3.0 on x86_64 NixOS unstable with kernel 6.6.8. The behaviour is identical on gcc trunk.=