[libcxx-dev] [cfe-dev] Exception handling with RTTI disabled on macOS

Marshall Clow via libcxx-dev libcxx-dev at lists.llvm.org
Tue Jan 26 07:44:10 PST 2021


[ removing cfe-dev]

On Jan 26, 2021, at 7:25 AM, Ilia K via cfe-dev <cfe-dev at lists.llvm.org> wrote:
> 
> Hi!
> 
> It turned out that I cannot catch `std::bad_alloc&` thrown by the `operator new` when compiling 1.cpp with -fno-rtti and linking against system's libc++.1.dylib/libc++abi.dylib on macOS. This scenario works on Linux, and according to the official libc++ page it is supported ("However linking against it with -fno-rtti is supported", https://libcxx.llvm.org/ <https://libcxx.llvm.org/>), however the following code is aborted due to an unhandled exception:
> 
> ```1.cpp
> #include <cstdio>
> #include <new>
> 
> int main() {
>     try {
>         ::operator new(-1);
>     } catch (std::bad_alloc& e) {
>         printf("caught %s\n", e.what());
>     }
>     return 0;
> }
> ```
> 
> Compile & run:
> ```
> $ clang++ -fno-rtti 1.cpp -o 1_no_rtti
> $ ./1_no_rtti
> libc++abi.dylib: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
> Abort trap: 6
> ```


That sounds right to me.
If you don’t have RTTI, then the catch mechanism cannot match up the type of the exception and the type that you’re looking to catch.
You should be able to catch (…), though.

— Marshall
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/libcxx-dev/attachments/20210126/53d4824c/attachment.html>


More information about the libcxx-dev mailing list