llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang] Should copy operations always need to be available for exception thrown?

Open trcrsired opened this issue 3 months ago • 2 comments

#include<exception>
#include<cstdlib>
#include<cstdio>

struct noncopymovableobject
{
        void *obj{};
        noncopymovableobject(noncopymovableobject const&) = delete;
        noncopymovableobject& operator=(noncopymovableobject const&) = delete;
        noncopymovableobject() noexcept
        {
                obj = malloc(4096);
                __builtin_printf("%s\n",__PRETTY_FUNCTION__);
        }
        noncopymovableobject(noncopymovableobject && other) noexcept:obj(other.obj)
        {
                other.obj = nullptr;
        }
        noncopymovableobject& operator=(noncopymovableobject && other) noexcept
        {
                if(__builtin_addressof(other)==this)
                        return *this;
                free(obj);
                __builtin_printf("%s\n",__PRETTY_FUNCTION__);
                obj=other.obj;
                other.obj=nullptr;
                return *this;
        }
        ~noncopymovableobject()
        {
                free(obj);
                __builtin_printf("%s\n",__PRETTY_FUNCTION__);
        }
};


int main()
try
{
        throw noncopymovableobject();
}
catch(...)
{
        std::exception_ptr eptr = ::std::current_exception();
}
[otgnhon test_eh_ptr]$ clang++ -o eh.exe eh.cc -O3 --config=$HOME/cfgs/x86_64-windows-msvc.cfg
[otgnhon test_eh_ptr]$ wine ./eh.exe
noncopymovableobject::noncopymovableobject()
noncopymovableobject::~noncopymovableobject()
noncopymovableobject::~noncopymovableobject()

See? we have a double free on windows-msvc target since unlike itanium, msvc exception_ptr has no refcounting.

https://cplusplus.github.io/CWG/issues/1863.html

I have found issue here in CWG as Defect Report.

I think we should reject exception being thrown if it's copy constructor is marked as deleted (or private?). Am i right?

trcrsired avatar Aug 19 '26 16:08 trcrsired

@zwuis CC

TPPPP72 avatar Aug 20 '26 05:08 TPPPP72

@llvm/issue-subscribers-clang-frontend

Author: cqwrteur (trcrsired)

```cpp #include<exception> #include<cstdlib> #include<cstdio>

struct noncopymovableobject { void *obj{}; noncopymovableobject(noncopymovableobject const&) = delete; noncopymovableobject& operator=(noncopymovableobject const&) = delete; noncopymovableobject() noexcept { obj = malloc(4096); __builtin_printf("%s\n",PRETTY_FUNCTION); } noncopymovableobject(noncopymovableobject && other) noexcept:obj(other.obj) { other.obj = nullptr; } noncopymovableobject& operator=(noncopymovableobject && other) noexcept { if(__builtin_addressof(other)==this) return *this; free(obj); __builtin_printf("%s\n",PRETTY_FUNCTION); obj=other.obj; other.obj=nullptr; return *this; } ~noncopymovableobject() { free(obj); __builtin_printf("%s\n",PRETTY_FUNCTION); } };

int main() try { throw noncopymovableobject(); } catch(...) { std::exception_ptr eptr = ::std::current_exception(); }


[otgnhon test_eh_ptr]$ clang++ -o eh.exe eh.cc -O3 --config=$HOME/cfgs/x86_64-windows-msvc.cfg [otgnhon test_eh_ptr]$ wine ./eh.exe noncopymovableobject::noncopymovableobject() noncopymovableobject::~noncopymovableobject() noncopymovableobject::~noncopymovableobject()


See? we have a double free on windows-msvc target since unlike itanium, msvc exception_ptr has no refcounting.

https://cplusplus.github.io/CWG/issues/1863.html

I have found issue here in CWG as Defect Report.

I think we should reject exception being thrown if it's copy constructor is marked as deleted (or private?). Am i right?
</details>