Skip to content

Commit

Permalink
Strengthen assertions on reflection methods.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mkruskal-google authored and copybara-github committed May 25, 2023
1 parent 65d987f commit eb5e9e0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/google/protobuf/generated_message_reflection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit eb5e9e0

Please sign in to comment.