diff --git a/src/google/protobuf/BUILD.bazel b/src/google/protobuf/BUILD.bazel index 3b6c05b1c1..38aa704845 100644 --- a/src/google/protobuf/BUILD.bazel +++ b/src/google/protobuf/BUILD.bazel @@ -1268,6 +1268,7 @@ cc_library( ":protobuf", "//src/google/protobuf/testing", "//src/google/protobuf/testing:file", + "@com_google_absl//absl/base:config", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/log:absl_check", "@com_google_absl//absl/strings", @@ -1735,6 +1736,7 @@ cc_test( "//src/google/protobuf/testing", "//src/google/protobuf/testing:file", "//src/google/protobuf/util:differencer", + "@com_google_absl//absl/base:config", "@com_google_absl//absl/hash:hash_testing", "@com_google_absl//absl/log:absl_check", "@com_google_absl//absl/log:scoped_mock_log", @@ -1766,6 +1768,7 @@ cc_test( "//src/google/protobuf/testing", "//src/google/protobuf/testing:file", "//src/google/protobuf/util:differencer", + "@com_google_absl//absl/base:config", "@com_google_absl//absl/log:absl_check", "@com_google_absl//absl/log:scoped_mock_log", "@com_google_absl//absl/strings", diff --git a/src/google/protobuf/lite_unittest.cc b/src/google/protobuf/lite_unittest.cc index 1e97ad01a1..7fb864e8bf 100644 --- a/src/google/protobuf/lite_unittest.cc +++ b/src/google/protobuf/lite_unittest.cc @@ -1395,16 +1395,23 @@ TEST(LiteTest, DynamicCastMessage) { EXPECT_EQ(shared_2, nullptr); } -#if GTEST_HAS_DEATH_TEST TEST(LiteTest, DynamicCastMessageInvalidReferenceType) { CastType1 test_type_1; const MessageLite& test_type_1_pointer_const_ref = test_type_1; +#if defined(ABSL_HAVE_EXCEPTIONS) + EXPECT_THROW(DynamicCastMessage(test_type_1_pointer_const_ref), + std::bad_cast); +#elif defined(GTEST_HAS_DEATH_TEST) ASSERT_DEATH( DynamicCastMessage(test_type_1_pointer_const_ref), absl::StrCat("Cannot downcast ", test_type_1.GetTypeName(), " to ", CastType2::default_instance().GetTypeName())); +#else + (void)test_type_1; + (void)test_type_1_pointer_const_ref; + GTEST_SKIP() << "Can't test the failure."; +#endif } -#endif // GTEST_HAS_DEATH_TEST TEST(LiteTest, DownCastMessageValidType) { CastType1 test_type_1; diff --git a/src/google/protobuf/message_lite.cc b/src/google/protobuf/message_lite.cc index 0b1ac2b79c..ac5d28d12a 100644 --- a/src/google/protobuf/message_lite.cc +++ b/src/google/protobuf/message_lite.cc @@ -19,8 +19,10 @@ #include #include #include +#include #include +#include "absl/base/config.h" #include "absl/log/absl_check.h" #include "absl/log/absl_log.h" #include "absl/strings/cord.h" @@ -198,6 +200,9 @@ void MessageLite::LogInitializationErrorMessage() const { namespace internal { void FailDynamicCast(const MessageLite& from, const MessageLite& to) { +#if defined(ABSL_HAVE_EXCEPTIONS) + throw std::bad_cast(); +#endif const auto to_name = to.GetTypeName(); if (internal::GetClassData(from)->is_dynamic) { ABSL_LOG(FATAL) diff --git a/src/google/protobuf/message_unittest.inc b/src/google/protobuf/message_unittest.inc index 03b381e386..b518c0bb40 100644 --- a/src/google/protobuf/message_unittest.inc +++ b/src/google/protobuf/message_unittest.inc @@ -35,6 +35,7 @@ #include #include "google/protobuf/testing/googletest.h" #include +#include "absl/base/config.h" #include "absl/log/absl_check.h" #include "absl/log/scoped_mock_log.h" #include "absl/strings/cord.h" @@ -816,11 +817,17 @@ TEST(MESSAGE_TEST_NAME, DynamicCastMessage) { TEST(MESSAGE_TEST_NAME, DynamicCastMessageInvalidReferenceType) { UNITTEST::TestAllTypes test_all_types; const MessageLite& test_all_types_pointer_const_ref = test_all_types; +#if defined(ABSL_HAVE_EXCEPTIONS) + EXPECT_THROW(DynamicCastMessage( + test_all_types_pointer_const_ref), + std::bad_cast); +#else ASSERT_DEATH( DynamicCastMessage( test_all_types_pointer_const_ref), absl::StrCat("Cannot downcast ", test_all_types.GetTypeName(), " to ", UNITTEST::TestRequired::default_instance().GetTypeName())); +#endif } TEST(MESSAGE_TEST_NAME, DownCastMessageValidType) {