Skip to content

clang fails to destroy return value when a destructor throws #12658

Open
@rjmccall

Description

@rjmccall
Bugzilla Link 12286
Version trunk
OS All
CC @DougGregor,@seanbaxter,@mizvekov,@tavianator,@yuanfang-chen

Extended Description

If an exception is thrown after a non-POD return value is successfully constructed in the return slot but before the returning function terminates, the value in the return slot is not destroyed. This can be clearly seen in the following test case. The really easy solution would be to push an inactive destructor cleanup in the prologue and activate it whenever evaluation completes for a return value, but that wouldn't be the correct ordering of destruction as specified in [except.ctor]p1. Needs more thought.

#include <stdio.h>

struct A {
  A() { printf("creating A at %p\n", this); }
  A(const A &a) { printf("copying A at %p into %p\n", &a, this); }
  ~A() { printf("destroying A at %p\n", this); }
};

struct B {
  B() { printf("entering B\n"); }
  ~B() { printf("exiting B, throwing!\n"); throw 0; }
};

A test() {
  B b;
  return A();
}

int main() {
  try {
    printf("running\n");
    A a = test();
  } catch (int i) {
    printf("caught\n");
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++clang:codegenIR generation bugs: mangling, exceptions, etc.confirmedVerified by a second party

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions