libstdc++: fix pointer type exception catch (no RTTI) [PR105387]
authorJakob Hasse <0xjakob@users.noreply.github.com>
Tue, 26 Apr 2022 04:03:47 +0000 (12:03 +0800)
committerJonathan Wakely <jwakely@redhat.com>
Sat, 5 Nov 2022 14:03:08 +0000 (14:03 +0000)
commitb83f01d0057578ebc1785f858fbfd46cdc210560
tree8c24796d5e642fb83dd49271cb7b29f76329b5f1
parent7c6008e75df80607f8104e665e0448a0a9cbf85a
libstdc++: fix pointer type exception catch (no RTTI) [PR105387]

__pbase_type_info::__do_catch(), used to catch pointer type exceptions,
did not check if the type info object to compare against is a pointer
type info object before doing a static down-cast to a pointer type info
object. If RTTI is disabled, this leads to the following situation:
Since a pointer type info object has additional fields, they would
end up being undefined if the actual type info object was not a pointer
type info object.

A simple check has been added before the down-cast happens.

Note that a consequence of this check is that exceptions of type
pointer-to-member cannot be caught anymore.

In case RTTI is enabled, this does not seem to be a problem because
RTTI-based checks would run before and prevent running into the bad
down-cast. Hence, the fix is disabled if RTTI is enabled and exceptions
of type pointer-to-member can still be caught.

libstdc++-v3/ChangeLog:

PR libstdc++/105387
* libsupc++/pbase_type_info.cc (__do_catch) [!__cpp_rtti]: Add
check that the thrown type is actually a pointer.
* testsuite/18_support/105387.cc: New test.
* testsuite/18_support/105387_memptr.cc: New test.

Signed-off-by: Jakob Hasse <jakob.hasse@espressif.com>
libstdc++-v3/libsupc++/pbase_type_info.cc
libstdc++-v3/testsuite/18_support/105387.cc [new file with mode: 0644]
libstdc++-v3/testsuite/18_support/105387_memptr.cc [new file with mode: 0644]