Skip to content

Commit

Permalink
Use an explicit union with commented explanation rather than casting
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 698437576
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Nov 20, 2024
1 parent 95c2cb8 commit 35dbd5c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
9 changes: 9 additions & 0 deletions upb/message/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <string.h>

#include "upb/base/string_view.h"
#include "upb/message/internal/types.h"

// Must be last.
#include "upb/port/def.inc"
Expand All @@ -41,6 +42,14 @@ typedef union {
// documentation in kUpb_DecodeOption_ExperimentalAllowUnlinked for more
// information.
uintptr_t tagged_msg_val; // upb_TaggedMessagePtr

// For an extension field, we are essentially treating ext->data (a
// upb_MessageValue) as if it were a message with one field that lives at
// offset 0. This works because upb_MessageValue is precisely one value that
// can hold any type of data. Recall that an extension can be of any type
// (scalar, repeated, or message). For a message extension, that will be a
// single upb_Message* at offset 0 of the upb_MessageValue.
struct upb_Message UPB_PRIVATE(ext_msg_val);
} upb_MessageValue;

UPB_API_INLINE upb_MessageValue upb_MessageValue_Zero(void) {
Expand Down
4 changes: 2 additions & 2 deletions upb/wire/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ static void upb_Decoder_AddKnownMessageSetItem(
}
upb_Message* submsg = _upb_Decoder_NewSubMessage2(
d, ext->ext->UPB_PRIVATE(sub).UPB_PRIVATE(submsg),
&ext->ext->UPB_PRIVATE(field), (upb_TaggedMessagePtr*)&ext->data);
&ext->ext->UPB_PRIVATE(field), &ext->data.tagged_msg_val);
upb_DecodeStatus status = upb_Decode(
data, size, submsg, upb_MiniTableExtension_GetSubMessage(item_mt),
d->extreg, d->options, &d->arena);
Expand Down Expand Up @@ -1182,7 +1182,7 @@ const char* _upb_Decoder_DecodeKnownField(upb_Decoder* d, const char* ptr,
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
d->original_msg = msg;
msg = (upb_Message*)&ext->data;
msg = &ext->data.UPB_PRIVATE(ext_msg_val);
if (upb_MiniTableField_IsSubMessage(&ext->ext->UPB_PRIVATE(field))) {
ext_sub.UPB_PRIVATE(submsg) =
&ext->ext->UPB_PRIVATE(sub).UPB_PRIVATE(submsg);
Expand Down
2 changes: 1 addition & 1 deletion upb/wire/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ static void encode_ext(upb_encstate* e, const upb_Extension* ext,
sub.UPB_PRIVATE(subenum) =
ext->ext->UPB_PRIVATE(sub).UPB_PRIVATE(subenum);
}
encode_field(e, (upb_Message*)&ext->data, &sub,
encode_field(e, &ext->data.UPB_PRIVATE(ext_msg_val), &sub,
&ext->ext->UPB_PRIVATE(field));
}
}
Expand Down

0 comments on commit 35dbd5c

Please sign in to comment.