<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/55340>55340</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
std::rethrow_exception and catch as const char*& gives invalid memory address
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
tonttu
</td>
</tr>
</table>
<pre>
Captured rethrown string literal seems to point to an unallocated memory location if capturing the string by reference.
If I change it to catch by value, or throw anything else except `const char*`, the code seems to work fine. It also compiles and runs correctly using gcc.
The problem with clang happens on both amd64 Linux and M1 macOS.
```c++
#include <exception>
#include <cstdio>
int main() {
std::exception_ptr err;
try {
throw "foo";
} catch (...) {
err = std::current_exception();
}
using E = const char *;
try {
std::rethrow_exception(err);
} catch (E& err) {
printf("%s\n", err);
}
}
```
The application prints `(null)`.
On Linux Valgrind complains:
```
==23939== Invalid read of size 8
==23939== at 0x400B53: main (in ./a.out)
==23939== Address 0x5b7fdc0 is 0 bytes after a block of size 112 alloc'd
==23939== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23939== by 0x4ECF9AD: __cxa_allocate_dependent_exception (eh_alloc.cc:315)
==23939== by 0x4ED0A82: std::rethrow_exception(std::__exception_ptr::exception_ptr) (eh_ptr.cc:229)
==23939== by 0x400B01: main (in ./a.out)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyNVV1v4jgU_TXm5aqRcUiBBx4oFKnSruZhVvuKHOdCvDV2ZDtt2V8_1yZAqdrOVJZrx_Y5535Su-a4WMku9h4b8Bhb714thOi13YPREb00EBAPAaKDzmkb00Ja6K00xikZ6eEBD84fIW-1s6B3oDJoQoktngHrI3Hs0KNVWDC-Znx5mp928ASqlXaPoDMDIak2PXiRpkcmVuA8ZHlEfoxtgkMTEPBNYReB3XPlbIgJxTOxpH16lMiVa_Bqw6v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