Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception safety bug in expected #43

Open
jwakely opened this issue Sep 9, 2021 · 0 comments
Open

Exception safety bug in expected #43

jwakely opened this issue Sep 9, 2021 · 0 comments

Comments

@jwakely
Copy link

jwakely commented Sep 9, 2021

#include "experimental/fundamental/v3/expected2/expected.hpp"
using std::experimental::expected;
#include <cstdio>

struct hurts
{
  hurts() { std::puts("ow that hurts"); }
  hurts(const hurts&) { std::puts("owow that hurts too"); }
  ~hurts() { puts("goodbye cruel world"); }
};

bool bring_the_pain;

struct uncopyable
{
  uncopyable() { }
  uncopyable(const uncopyable&) { if (bring_the_pain) throw "pain!"; }
};

int main()
{
  expected<uncopyable, hurts> expo;
  try
  {
    bring_the_pain = true;
    auto expono = expo;
  }
  catch (...)
  { }
}

This prints:

goodbye cruel world

That comes from the destructor of an object that was never constructed, i.e. undefined behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant