Skip to content

dtor of return value isn't run if dtor of a scoped variable throws and is caught in the same function #73024

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

Closed
kelbon opened this issue Nov 21, 2023 · 5 comments
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. duplicate Resolved as duplicate miscompilation

Comments

@kelbon
Copy link
Contributor

kelbon commented Nov 21, 2023

https://godbolt.org/z/nYnvv3EEf

Code without address sanitizer to avoid false positive:

https://godbolt.org/z/qbxMG659h

#include <vector>

inline bool g = false;

struct X {
    int i = 0;

    ~X() {
        if (i == 1) g = true;
    }
};
struct Y {
    ~Y() noexcept(false) { throw 0; }
};
X f() {
    try {
        Y y;
        return X{1};
    } catch (...) {
    }
    return X{};
}

int main() {
    f();
    if (!g)
      return 4;
}
@github-actions github-actions bot added the clang Clang issues not falling into any other category label Nov 21, 2023
@EugeneZelenko EugeneZelenko added clang:codegen IR generation bugs: mangling, exceptions, etc. and removed clang Clang issues not falling into any other category labels Nov 21, 2023
@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2023

@llvm/issue-subscribers-clang-codegen

Author: None (kelbon)

https://godbolt.org/z/nYnvv3EEf

Code without address sanitizer to avoid false positive:

https://godbolt.org/z/qbxMG659h

#include &lt;vector&gt;

inline bool g = false;

struct X {
    int i = 0;

    ~X() {
        if (i == 1) g = true;
    }
};
struct Y {
    ~Y() noexcept(false) { throw 0; }
};
X f() {
    try {
        Y y;
        return X{1};
    } catch (...) {
    }
    return X{};
}

int main() {
    f();
    if (!g)
      return 4;
}

@dwblaikie
Copy link
Collaborator

Can't say I understand the non-asan failure. It looks correct, but I'm probably misunderstanding...

In https://godbolt.org/z/qxTjva39r - the Y dtor runs, so X{1} does /not/ execute, but X{} does execute, so g would still be false, and main would return 4, right? That seems correct?

@kelbon
Copy link
Contributor Author

kelbon commented Nov 23, 2023

Can't say I understand the non-asan failure. It looks correct, but I'm probably misunderstanding...

In https://godbolt.org/z/qxTjva39r - the Y dtor runs, so X{1} does /not/ execute, but X{} does execute, so g would still be false, and main would return 4, right? That seems correct?

No, its not correct, value was constructed, but not destroyed, basic RAII broken

https://godbolt.org/z/sqfeT7jPh

@dwblaikie
Copy link
Collaborator

Yeah, fair enough, that last one seems unambiguously buggy to me.

@dwblaikie dwblaikie changed the title clang generates invalid code after exception in dctor dtor of return value isn't run if dtor of a scoped variable throws and is caught in the same function Nov 25, 2023
@shafik
Copy link
Collaborator

shafik commented Nov 28, 2023

I believe this is a duplicate of: #12658

@shafik shafik closed this as not planned Won't fix, can't repro, duplicate, stale Nov 28, 2023
@shafik shafik added the duplicate Resolved as duplicate label Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. duplicate Resolved as duplicate miscompilation
Projects
None yet
Development

No branches or pull requests

6 participants