llvm / llvm/llvm-project

[bug][clang][windows] clang-cl 22 stack overflow when using multi-level `std::throw_with_nested`

Open
#191,462 4 comments 0 reactions 0 assignees View on GitHub
clang:codegen diverges-from:msvc miscompilation platform:windows
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

## Summary

Multi-level nested exceptions (2+ levels of `std::throw_with_nested`) with clang-cl 22 on Windows (MSVC STL) causes infinite recursion and stack overflow.
This also happens when using clang (not clang-cl).

## Environment

- **Clang Version**: 22.1.3 (targeting x86_64-pc-windows-msvc)
- **MSVC Runtime**: Tested with 14.44.35211.0 and 14.50.35719.0 (both fail)
- **OS**: Windows 11 Enterprise Build 26100
- **Compiler Flags**:
- `/EHsc` (Exceptions enabled)

## Reproduction

Single-level nesting: ✅ WORKS
Double-level nesting: ❌ CRASHES

### Minimal Test Case

```cpp
#include
#include
#include

int
main()
{
try
{
try
{
try
{
throw std::exception();
}
catch (...)
{
std::throw_with_nested(std::exception());
}
}
catch (...)
{
std::throw_with_nested(std::exception());
}
}
catch (...)
{
auto ptr = std::current_exception();
std::cout << "Retrieved exception pointer from current_exception().\n";
}
return 0;
}
```

### Compile Command

```bash
clang-cl /EHsc test.cpp
```

### Expected Behavior

Program should catch the nested exception and print `Retrieved exception pointer from current_exception().`

### Actual Behavior

Infinite recursion in exception handling, stack overflow after a lot of frames:

```raw
* thread #1, stop reason = Exception 0xc0000005 encountered at address 0x7ffb9e910a24: Access violation writing location 0xdf86e00f88
* frame #0: 0x00007ffb9e910a24 ntdll.dll`RtlSetBits + 3604
frame #1: 0x00007ffb9e8dd151 ntdll.dll`RtlAllocateHeap + 3841
frame #2: 0x00007ffb9e8dcd24 ntdll.dll`RtlAllocateHeap + 2772
frame #3: 0x00007ffb9e910388 ntdll.dll`RtlSetBits + 1912
frame #4: 0x00007ffb9e912e5a ntdll.dll`RtlSetBits + 12874
frame #5: 0x00007ffb9e8dd151 ntdll.dll`RtlAllocateHeap + 3841
frame #6: 0x00007ffb9e8dcd24 ntdll.dll`RtlAllocateHeap + 2772
frame #7: 0x00007ff7b7aa31e4 test.exe`
frame #8: 0x00007ff7b7a99b2d test.exe`
frame #9: 0x00007ff7b7a912aa test.exe`
frame #10: 0x00007ff7b7a91242 test.exe`
[goes infinite from here...]
frame #14046: 0x00007ff7b7aa05c7 test.exe`
frame #14047: 0x00007ffb9e9e1386 ntdll.dll`RtlCaptureContext2 + 1190
frame #14048: 0x00007ff7b7a91043 test.exe`
frame #14049: 0x00007ff7b7a987cc test.exe`
frame #14050: 0x00007ffb9d44e8d7 kernel32.dll`BaseThreadInitThunk + 23
frame #14051: 0x00007ffb9e94c48c ntdll.dll`RtlUserThreadStart + 44
```

Contributor guide

Open the contributing guide

Research direction

Start with the minimal reproduction in test.cpp and run it with clang-cl /EHsc on Windows using the reported Clang and MSVC runtime versions. Compare single-level and multi-level std::throw_with_nested behavior; done means the multi-level case catches the nested exception, prints the expected message, and no longer recurses until stack overflow.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.