public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/112301] New: Double destruction of returned object when exiting the scope causes an exception which gets rethrown
@ 2023-10-30 16:14 alexander.grund@tu-dresden.de
  2023-10-30 16:29 ` [Bug c++/112301] [Regression 12/13/14] " redi at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: alexander.grund@tu-dresden.de @ 2023-10-30 16:14 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112301
           Summary: Double destruction of returned object when exiting the
                    scope causes an exception which gets rethrown
           Product: gcc
           Version: 12.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: alexander.grund@tu-dresden.de
  Target Milestone: ---

Created attachment 56476
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=56476&action=edit
More complete example with logging pointers

I debugged a heap corruption I traced back to a use-after-free caused by an
extra destructor call.

I suspect the cause could be the fix for
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=33799 where a throwing destructor
led to a missing destructor call.
It could be similar to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=12751 which
also had an extra destructor call generated for an already destructed instance.

Minimized code sample:

#include <stdexcept>
#include <cassert>

int num = 0;
struct ptr{
    ptr(){
        ++num;
    }
    ptr(ptr&&){
        ++num;
    }
    ~ptr(){
        assert(num-- > 0);
    }
};

struct ThrowOnExit{
    ~ThrowOnExit() noexcept(false){
        throw std::runtime_error("");
    }
};

ptr foo(ptr x){
    try{
        ThrowOnExit _;
        return x;
    }catch (const std::exception&) {
        throw;
    }
}

void wrapper(){
    try{
        foo(ptr{});
    }catch(const std::exception&){}
}

int main(){
    wrapper();
}


The assertion fails, although it should not. Logging the constructions and
destructions and removing the assert gives me this:

construct 0x7ffd4538088e
move construct 0x7ffd4538088f
free 0x7ffd4538088f
free 0x7ffd4538088f
free 0x7ffd4538088e

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2024-03-04  4:26 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-30 16:14 [Bug c++/112301] New: Double destruction of returned object when exiting the scope causes an exception which gets rethrown alexander.grund@tu-dresden.de
2023-10-30 16:29 ` [Bug c++/112301] [Regression 12/13/14] " redi at gcc dot gnu.org
2023-10-30 21:45 ` jason at gcc dot gnu.org
2023-11-02 20:01 ` [Bug c++/112301] [12/13/14 regression] Double destruction of returned object when exiting the scope causes an exception which gets rethrown since r12-6333-gb10e031458d541 cvs-commit at gcc dot gnu.org
2023-11-02 20:03 ` jason at gcc dot gnu.org
2023-11-03 10:01 ` alexander.grund@tu-dresden.de
2023-11-17  0:21 ` cvs-commit at gcc dot gnu.org
2023-11-17  0:21 ` cvs-commit at gcc dot gnu.org
2024-01-12  8:27 ` rguenth at gcc dot gnu.org
2024-01-12  8:32 ` pinskia at gcc dot gnu.org
2024-03-04  4:26 ` law at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).