std::rethrow_exception and catch as const char*& gives invalid memory address #55340
Open
Description
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.
#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)
Activity