[clang] [analyzer] Do not destruct fields of unions (PR #122330)
Gábor Horváth via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 4 02:57:13 PST 2025
================
@@ -441,3 +441,31 @@ void testLeakBecauseNTTPIsNotDeallocation() {
void* p = ::operator new(10);
deallocate_via_nttp<not_free>(p);
} // leak-warning{{Potential leak of memory pointed to by 'p'}}
+
+namespace optional_union {
+ template <typename T>
+ class unique_ptr {
+ T *q;
+ public:
+ unique_ptr() : q(new T) {}
+ ~unique_ptr() {
+ delete q;
+ }
+ };
+
+ union custom_union_t {
+ unique_ptr<int> present;
+ char notpresent;
+ custom_union_t() : present(unique_ptr<int>()) {}
+ ~custom_union_t() {};
+ };
+
+ void testUnionCorrect() {
+ custom_union_t a;
+ a.present.~unique_ptr<int>();
+ }
+
+ void testUnionLeak() {
----------------
Xazax-hun wrote:
Could we also have a test where the custom union does not have a dtor declared for completeness? Or is that already covered somewhere?
https://github.com/llvm/llvm-project/pull/122330
More information about the cfe-commits
mailing list