Skip to content

Exception safety bug in expected #43

@jwakely

Description

@jwakely
#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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions