public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "david.cortes.rivera at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/102237] New: longjmp leaks catched std::runtime_error
Date: Wed, 08 Sep 2021 02:19:21 +0000	[thread overview]
Message-ID: <bug-102237-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 102237
           Summary: longjmp leaks catched std::runtime_error
           Product: gcc
           Version: 10.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: david.cortes.rivera at gmail dot com
  Target Milestone: ---

Created attachment 51425
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51425&action=edit
ii_file

I am experiencing a memory leak when using long jumps from a catch block in
which the jump takes to a different function. Basically, the exception objects
are not getting destructed.

>From what I gather from this SO question:
https://stackoverflow.com/questions/69092014/c-will-an-stdruntime-error-object-leak-in-a-longjmp

calling something like "catch(std::exception &e) {longjmp(jump_buffer, 1);}"
should be allowed by the standard, and the exception should be destructed along
the way.

For example, the following code ***works as expected***:
-------
#include <stdexcept>
#include <stdio.h>
#include <setjmp.h>

void my_fun()
{
    jmp_buf jump_buffer;
    if (setjmp(jump_buffer))
        return;

    try {
        std::string message;
        message.resize(100);
        snprintf(&message[0], 100, "error code %d\n", 3);
        throw std::runtime_error(message);
    }

    catch (std::runtime_error &e) {
        longjmp(jump_buffer, 1);
    }
}

int main()
{
    for (int ix = 0; ix < 100000; ix++)
        void my_fun();
    return 0;
}
-------

...

But ***if I switch the jump buffer to be outside of the function***, it will
now start leaking memory at each call:
-------
#include <stdexcept>
#include <stdio.h>
#include <setjmp.h>

jmp_buf jump_buffer;
void my_fun()
{
    try {
        std::string message;
        message.resize(100);
        snprintf(&message[0], 100, "error code %d\n", 3);
        throw std::runtime_error(message);
    }

    catch (std::runtime_error &e) {
        longjmp(jump_buffer, 1);
    }
}

void call_myfun()
{
    if (setjmp(jump_buffer))
        return;
    my_fun();
}

int main()
{
    for (int ix = 0; ix < 100000; ix++)
        call_myfun();
    return 0;
}
-------


Setup info:
- AMD Ryzen 7 2700.
- Debian linux (sid)
- gcc --version: gcc (Debian 10.3.0-9) 10.3.0

The source file is compiled with the default options (g++ file.cpp). The leak
is detected by valgrind-3.16.1.

Attached is the .ii file.

             reply	other threads:[~2021-09-08  2:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-08  2:19 david.cortes.rivera at gmail dot com [this message]
2021-09-08  2:46 ` [Bug c++/102237] [DR2361] " pinskia at gcc dot gnu.org
2021-09-08  2:47 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-102237-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).