gcc/libstdc++-v3/testsuite/18_support/105387_memptr.cc
Jakob Hasse b83f01d005 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>
2022-11-05 14:03:08 +00:00

25 lines
459 B
C++

#include <testsuite_hooks.h>
// Test related to PR libstdc++/105387
// Check that pointer-to-member type exceptions can still be caught with -frtti.
// { dg-require-effective-target rtti }
void test_catch_ptr_to_member()
{
bool exception_thrown = false;
struct X { int i; };
try {
throw &X::i;
}
catch (const int X::*) {
exception_thrown = true;
}
VERIFY(exception_thrown);
}
int main()
{
test_catch_ptr_to_member();
return 0;
}