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
pull/12898/head
Mike Kruskal 2 years ago committed by Copybara-Service
parent 65d987f802
commit eb5e9e01d9
  1. 4
      src/google/protobuf/generated_message_reflection.cc
  2. 2
      src/google/protobuf/generated_message_reflection_unittest.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

@ -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,

Loading…
Cancel
Save