<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/120987>120987</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [TySan] false positive with unions and placement-new
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            false-positive
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Jannik2099
      </td>
    </tr>
</table>

<pre>
    According to https://eel.is/c++draft/class.union#general-6 , the active union member in an union of non-POD types can be switched by invoking the dtor of the old member, and placement new-ing into the new member.

However, tysan does not seem to like this https://godbolt.org/z/nvnjzjGez

```cpp
#include <cstdio>
#include <new>

class Foo {
   private:
    short i{};

 public:
    Foo() { printf("Foo nontrivial ctor\n"); }
    ~Foo() { printf("Foo nontrivial dtor\n"); }
};

class Bar {
   private:
 int i{};

   public:
    Bar() { printf("Bar nontrivial ctor\n"); }
 ~Bar() { printf("Bar nontrivial dtor\n"); }
};

union U {
    Foo foo;
    Bar bar;
    ~U() {}
};

int main() { 
    U u{ .foo = {}};
 u.foo.~Foo();
    new (&u.bar) Bar;
}
```

```
==1==ERROR: TypeSanitizer: type-aliasing-violation on address 0x7ffff9117e2c (pc 0x61eed1b9b89d bp 0x7ffff9117d10 sp 0x7ffff9117cb8 tid 1)
WRITE of size 4 at 0x7ffff9117e2c with type int (in Bar at offset 0) accesses an existing object of type short (in Foo at offset 0)
    #0 0x61eed1b9b89c (/app/output.s+0x2b89c)
```

This also happens with an union of POD or trivial types, but I'm not 100% sure on the standard working if placement-new is supposed to be allowed in these cases.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyMVU1v4zgP_jXKhYhhy_nyIYdkMnnf2cssOh3smZboWFNFMiQ5aXrob1_QTpF-bIECAWJR5MNPPcQYzcERrcV8K-a7Cfap9WH9FzpnHmReVZPa68t6o5QP2rgDJA9tSl0U5UbIvZB7IpuZKOReC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