<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/208290>208290</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang 23 crashes with template friends
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang:frontend,
crash
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
avikivity
</td>
</tr>
</table>
<pre>
# Clang crashes (heap corruption) when a class template befriends a member-class-template specialization that then accesses a private member
## Summary
When a class template `data<K>` befriends a *specialization of a member class
template* of another class template — `friend struct tree<K>::template ib<true>;`
— and that specialization is instantiated and actually exercises the friendship
(accesses a private member of `data`), clang corrupts the heap and aborts.
## Reproducer
```cpp
template <typename K> class data;
template <typename K>
struct tree {
template <bool Const>
struct ib {
int get(data<K>* d) { return d->value; } // uses friendship
};
};
template <typename K>
class data {
friend struct tree<K>::template ib<true>; // befriend member-class-template specialization
int value;
};
template struct tree<int>::ib<true>; // instantiate -> crash
```
## Command
```
clang++ -std=c++23 -fsyntax-only friend-member-template-crash.cpp
```
## Actual behavior
The compiler aborts (SIGABRT, exit 134) with a "PLEASE submit a bug report"
message. It is 100% reproducible. In a larger TU it manifests instead as
SIGSEGV mid-compilation — the signature of heap corruption, so the exact fault
address is allocation-dependent. This is a release build (no assertions); an
assertions/ASan build would pinpoint the faulting code.
## Expected behavior
Compiles cleanly. The friend declaration is well-formed and grants `ib<true>`
access to `data::value`.
## What narrows it down
- Removing the `friend` line → compiles fine.
- Making `value` **public** (so friendship is never checked) → compiles fine.
- Never instantiating `tree<int>::ib<true>` → compiles fine.
So the crash is specifically in the friendship access-granting path for a
**member-class-template specialization** named as a friend of a class template.
## Version
```
clang version 23.0.0git (https://github.com/llvm/llvm-project.git 290f3bc2d12e5875a0be386fad8f6a5dfce9d6a3)
Target: x86_64-unknown-linux-gnu
```
Also reproduces with `-std=c++17` and `-std=c++20`.
</pre>
<img width="1" height="1" alt="" src="http://email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