Skip to content

Commit eb5e9e0

Browse files
mkruskal-googlecopybara-github
authored andcommitted
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
1 parent 65d987f commit eb5e9e0

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

src/google/protobuf/generated_message_reflection.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ void ReportReflectionUsageMessageError(const Descriptor* expected,
213213
" Expected type: %s\n"
214214
" Actual type : %s\n"
215215
" Field : %s\n"
216-
" Problem : Message is not the right type for reflection",
216+
" Problem : Message is not the right object for reflection",
217217
method, expected->full_name(), actual->full_name(), field->full_name());
218218
}
219219
#endif
@@ -288,7 +288,7 @@ static void ReportReflectionUsageEnumTypeError(
288288
#define USAGE_CHECK_MESSAGE(METHOD, MESSAGE)
289289
#else
290290
#define USAGE_CHECK_MESSAGE(METHOD, MESSAGE) \
291-
if (descriptor_ != (MESSAGE)->GetDescriptor()) \
291+
if (this != (MESSAGE)->GetReflection()) \
292292
ReportReflectionUsageMessageError(descriptor_, (MESSAGE)->GetDescriptor(), \
293293
field, #METHOD)
294294
#endif

src/google/protobuf/generated_message_reflection_unittest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ TEST(GeneratedMessageReflectionTest, UsageErrors) {
13311331
" Expected type: protobuf_unittest.TestAllTypes\n"
13321332
" Actual type : protobuf_unittest.ForeignMessage\n"
13331333
" Field : protobuf_unittest.TestAllTypes.optional_int32\n"
1334-
" Problem : Message is not the right type for reflection");
1334+
" Problem : Message is not the right object for reflection");
13351335
EXPECT_DEATH(
13361336
reflection->GetInt32(
13371337
message,

0 commit comments

Comments
 (0)