Skip to content

Commit

Permalink
Implement unknown field aliasing support
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 708077050
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Dec 20, 2024
1 parent f135ded commit efbd632
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
3 changes: 2 additions & 1 deletion upb/message/internal/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ void _upb_Message_DiscardUnknown_shallow(struct upb_Message* msg);

// Adds unknown data (serialized protobuf data) to the given message. The data
// must represent one or more complete and well formed proto fields.
// The data is copied into the message instance.
// If alias is set, will keep a view to the provided data; otherwise a copy is
// made.
bool UPB_PRIVATE(_upb_Message_AddUnknown)(struct upb_Message* msg,
const char* data, size_t len,
upb_Arena* arena, bool alias);
Expand Down
17 changes: 12 additions & 5 deletions upb/message/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,18 @@ bool UPB_PRIVATE(_upb_Message_AddUnknown)(upb_Message* msg, const char* data,
if (!UPB_PRIVATE(_upb_Message_ReserveSlot)(msg, arena)) {
return false;
}
upb_StringView* view = upb_Arena_Malloc(arena, sizeof(upb_StringView) + len);
if (!view) return false;
char* copy = UPB_PTR_AT(view, sizeof(upb_StringView), char);
memcpy(copy, data, len);
view->data = copy;
upb_StringView* view;
if (alias) {
view = upb_Arena_Malloc(arena, sizeof(upb_StringView));
if (!view) return false;
view->data = data;
} else {
view = upb_Arena_Malloc(arena, sizeof(upb_StringView) + len);
if (!view) return false;
char* copy = UPB_PTR_AT(view, sizeof(upb_StringView), char);
memcpy(copy, data, len);
view->data = copy;
}
view->size = len;
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
in->aux_data[in->size++] = upb_TaggedAuxPtr_MakeUnknownData(view);
Expand Down
4 changes: 2 additions & 2 deletions upb/wire/decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ extern "C" {
#endif

enum {
/* If set, strings will alias the input buffer instead of copying into the
* arena. */
/* If set, strings and unknown fields will alias the input buffer instead of
* copying into the arena. */
kUpb_DecodeOption_AliasString = 1,

/* If set, the parse will return failure if any message is missing any
Expand Down

0 comments on commit efbd632

Please sign in to comment.