Skip to content

Two object constructed at the same location in stmt-expr in a return stmt #91761

Open
@higher-performance

Description

@higher-performance

It seems #63818/#89154 haven't been quite fixed yet. In some sense, I think the fix actually made things worse, because we now get a double-destruction (which is more likely to cause a security issue) instead of neglecting to run a destructor (which would've been more likely to cause a leak instead).

Repro:

#include <stdio.h>
struct D {
    ~D()              { printf("[%p] D::~D()\n"           , this); }
    D()               { printf("[%p] D::D()\n"            , this); }
    D(int x)          { printf("[%p] D::D(int %d)\n"      , this, x); }
    D(D const &other) { printf("[%p] D::D(D const & %p)\n", this, &other); }
};
struct S { D d; int i; };
static S f() { return S{ D(1), ({ return S(); 0; }) }; }
int main() { return f().i; }

Clang (trunk):

[0x7ffdfbc4df68] D::D(int 1)
[0x7ffdfbc4df68] D::D()
[0x7ffdfbc4df68] D::~D()
[0x7ffdfbc4df68] D::~D()

Clang 18.1.0:

[0x7fffa42f9e60] D::D(int 1)
[0x7fffa42f9e60] D::D()
[0x7fffa42f9e60] D::~D()

Expected behavior: The D(1) subobject should be destroyed before S() constructs an object on top of it.

Actual behavior: The object is constructed twice at the same location, then destroyed twice at the same location.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:codegenIR generation bugs: mangling, exceptions, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions