From eb5e9e01d9831e9fae48bc6f688114f53cccabc5 Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Thu, 25 May 2023 12:55:24 -0700 Subject: [PATCH] Strengthen assertions on reflection methods. This will ensure that the message is using the correct reflection object, rather than just the correct descriptor. This distinction only matters with dynamic messages, where multiple reflection objects can exist for the same descriptor. Failing to pass this check can lead to UB and crashes. PiperOrigin-RevId: 535351694 --- src/google/protobuf/generated_message_reflection.cc | 4 ++-- src/google/protobuf/generated_message_reflection_unittest.cc | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/google/protobuf/generated_message_reflection.cc b/src/google/protobuf/generated_message_reflection.cc index b563c459cecd..623b27a07794 100644 --- a/src/google/protobuf/generated_message_reflection.cc +++ b/src/google/protobuf/generated_message_reflection.cc @@ -213,7 +213,7 @@ void ReportReflectionUsageMessageError(const Descriptor* expected, " Expected type: %s\n" " Actual type : %s\n" " Field : %s\n" - " Problem : Message is not the right type for reflection", + " Problem : Message is not the right object for reflection", method, expected->full_name(), actual->full_name(), field->full_name()); } #endif @@ -288,7 +288,7 @@ static void ReportReflectionUsageEnumTypeError( #define USAGE_CHECK_MESSAGE(METHOD, MESSAGE) #else #define USAGE_CHECK_MESSAGE(METHOD, MESSAGE) \ - if (descriptor_ != (MESSAGE)->GetDescriptor()) \ + if (this != (MESSAGE)->GetReflection()) \ ReportReflectionUsageMessageError(descriptor_, (MESSAGE)->GetDescriptor(), \ field, #METHOD) #endif diff --git a/src/google/protobuf/generated_message_reflection_unittest.cc b/src/google/protobuf/generated_message_reflection_unittest.cc index 29e584355493..62f02189de07 100644 --- a/src/google/protobuf/generated_message_reflection_unittest.cc +++ b/src/google/protobuf/generated_message_reflection_unittest.cc @@ -1331,7 +1331,7 @@ TEST(GeneratedMessageReflectionTest, UsageErrors) { " Expected type: protobuf_unittest.TestAllTypes\n" " Actual type : protobuf_unittest.ForeignMessage\n" " Field : protobuf_unittest.TestAllTypes.optional_int32\n" - " Problem : Message is not the right type for reflection"); + " Problem : Message is not the right object for reflection"); EXPECT_DEATH( reflection->GetInt32( message,