<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/75275>75275</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
crash with identical named types in anonymous namespace
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
firewave
</td>
</tr>
</table>
<pre>
The attached code is crashing when compiled with latest Clang.
```
$ clang test.cpp a.cpp b.cpp -lstdc++
$ ./a.out
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
```
It reproduces at least back to version 10 (the earliest I have available).
In it there are types with identical names in several sources which are stored via a base pointer outside of them and then processed again by casting them back.
If you rename the objects or place them in different namespaces the issue disappears. It starts happening as soon as two objects share the same name. So it seems similar to an ODR violation.
The code is working fine with GCC 10, 11, 12, 13 and Visual Studio 2022. There are also no issues reported by either valgrind or sanitizers with GCC.
See https://trac.cppcheck.net/ticket/12108 for the original report with additional details.
[example.zip](https://github.com/llvm/llvm-project/files/13655376/example.zip)
<details>
<summary>valgrind</summary>
```
==23443== Invalid read of size 8
==23443== at 0x48476BC: memcpy@GLIBC_2.2.5 (vg_replace_strmem.c:1134)
==23443== by 0x49B4E76: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*, std::forward_iterator_tag) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.32)
==23443== by 0x10A1A5: B::use(std::__cxx11::list<C::Obj*, std::allocator<C::Obj*> > const&) (in /mnt/s/clion/example_lite_3/a.out)
==23443== by 0x10929D: main (in /mnt/s/clion/example_lite_3/a.out)
==23443== Address 0x51aa038 is 8 bytes before a block of size 81,510,545 alloc'd
==23443== at 0x4840F01: operator new(unsigned long) (vg_replace_malloc.c:434)
==23443== by 0x49B4E5B: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*, std::forward_iterator_tag) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.32)
==23443== by 0x10A1A5: B::use(std::__cxx11::list<C::Obj*, std::allocator<C::Obj*> > const&) (in /mnt/s/clion/example_lite_3/a.out)
==23443== by 0x10929D: main (in /mnt/s/clion/example_lite_3/a.out)
```
</details>
<details>
<summary>AddressSanitizer</summary>
```
==23415==ERROR: AddressSanitizer: requested allocation size 0x604000000061 (0x604000001068 after adjustments for alignment, red zones etc.) exceeds maximum supported size of 0x10000000000 (thread T0)
#0 0x7faf99f97ff2 in malloc (/mnt/s/clion/example_lite_3/a.out+0xb9ff2) (BuildId: 3bc52bf1bc60839a320aa7e52f029979af156548)
#1 0x7faf99cd071b in operator new(unsigned long) (/lib/x86_64-linux-gnu/libstdc++.so.6+0xb071b) (BuildId: ab1b320aee6702c149c94e588317d8f66b7ab97a)
==23415==HINT: if you don't care about these errors you may set allocator_may_return_null=1
SUMMARY: AddressSanitizer: allocation-size-too-big (/mnt/s/clion/example_lite_3/a.out+0xb9ff2) (BuildId: 3bc52bf1bc60839a320aa7e52f029979af156548) in malloc
```
</details>
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsV1uP27oR_jXcl8EKEmXdHvzg9canCzQNkKQF-mSMyJHFhCJdkvIlv76g5LWzyZ5iUxzgvBzDoCl6NPz4zYUz6L3aGaIlKx5Y8XiHY-itW3bK0REPdNdaeV5-7gkwBBQ9SRBWEigPwqHvldnBsScDwg57pUnCUYUeNAbyAdYazS5h6SNLV5exTC_f-ZEvQEQhiPKJ2O8Bp7GdxnvtgxSMP8Tv9YWE8Q0mdgzzUiA3KIOBQKCOCLAL5CD0zh4jPDSgjA9oBIHtgPHKB8nyFctXLcotam0F49WsDODYY2C8Zrxh-QrgFdlXDzKNTwEc7Z2VoyAPGEAT-gAtiq8QLBzIeWUNZCkwXoeegNBpFZl6gh4PBHhApbHVxHjzgrcnAypA6MkRoCMI5z35mWslyQQlUIPBgTwoA54O5FCDt6OLSI69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