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

    <tr>
        <th>Summary</th>
        <td>
            Incorrect use of memcpy in default assignment operator
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          zim-tis
      </td>
    </tr>
</table>

<pre>
    The AST of  Foo::operator = (const Foo &) in the following test:

```
struct Foo {
    int t[1000] = {};
};

int main() {
  Foo f;
  f = f;
}
```

obtained with the command line: 

`
clang++ -fsyntax-only -Xclang -ast-dump test.cpp
`

uses `__builtin_memcpy` in its AST to perform the member copy:

|-CXXRecordDecl 0x5b11b80 <jeanyves.cpp:1:1, line:3:1> line:1:8 referenced struct Foo definition
[...]
| |-CXXMethodDecl 0x5b12698 <col:8> col:8 implicit used constexpr operator= 'Foo &(const Foo &) noexcept' inline default trivial
| | |-ParmVarDecl 0x5b127a8 <col:8> col:8 used 'const Foo &'
| | |-CompoundStmt 0x5b406f0 <col:8>
| |   |-CallExpr 0x5b40650 <col:8> 'void *'
| |   | |-ImplicitCastExpr 0x5b40638 <col:8> 'void *(*)(void *, const void *, unsigned long) noexcept' <BuiltinFnToFnPtr>
| |   | | -DeclRefExpr 0x5b405a0 <col:8> '<builtin fn type>' Function 0x5b40330 '__builtin_memcpy' 'void *(void *, const void *, unsigned long) noexcept
[...]

However, calling `memcpy`   is an undefined behavior when the origin and the source overlap, while the test is valid C++.
This link: https://godbolt.org/z/Mj61jzTnj shows that in the final assembly `__builtin_memcpy` is replaced by the standard `memcpy`.

A quick fix could be to use `memmove`/`__builtin_memmove` to do the copy.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVU-P26wT_jTkMkpEILGdQw75U-vXQ6WqXf3UW4XxOGaLwS_gZNNP_wrs3SbZ7eWVYmID88wzw8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