Skip to content

Commit

Permalink
Always add unknown fields in a single call
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 696895119
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Nov 15, 2024
1 parent c8add6c commit 1863e58
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 39 deletions.
17 changes: 0 additions & 17 deletions csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs

This file was deleted.

11 changes: 10 additions & 1 deletion upb/message/internal/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,21 @@ UPB_INLINE struct upb_Message* _upb_Message_New(const upb_MiniTable* m,
// Discards the unknown fields for this message only.
void _upb_Message_DiscardUnknown_shallow(struct upb_Message* msg);

// Adds unknown data (serialized protobuf data) to the given message.
// 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.
bool UPB_PRIVATE(_upb_Message_AddUnknown)(struct upb_Message* msg,
const char* data, size_t len,
upb_Arena* arena);

// Adds unknown data (serialized protobuf data) to the given message.
// The data is copied into the message instance. Data when concatenated together
// must represent one or more complete and well formed proto fields, but the
// individual spans may point only to partial fields.
bool UPB_PRIVATE(_upb_Message_AddUnknownV)(struct upb_Message* msg,
upb_Arena* arena,
upb_StringView data[], size_t count);

bool UPB_PRIVATE(_upb_Message_Realloc)(struct upb_Message* msg, size_t need,
upb_Arena* arena);

Expand Down
25 changes: 25 additions & 0 deletions upb/message/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "upb/message/message.h"

#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
Expand Down Expand Up @@ -38,13 +39,37 @@ upb_Message* upb_Message_New(const upb_MiniTable* m, upb_Arena* a) {
bool UPB_PRIVATE(_upb_Message_AddUnknown)(upb_Message* msg, const char* data,
size_t len, upb_Arena* arena) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
// TODO: b/376969853 - Add debug check that the unknown field is an overall
// valid proto field
if (!UPB_PRIVATE(_upb_Message_Realloc)(msg, len, arena)) return false;
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
memcpy(UPB_PTR_AT(in, in->unknown_end, char), data, len);
in->unknown_end += len;
return true;
}

bool UPB_PRIVATE(_upb_Message_AddUnknownV)(struct upb_Message* msg,
upb_Arena* arena,
upb_StringView data[],
size_t count) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
UPB_ASSERT(count > 0);
size_t total_len = 0;
for (size_t i = 0; i < count; i++) {
total_len += data[i].size;
}
if (!UPB_PRIVATE(_upb_Message_Realloc)(msg, total_len, arena)) return false;

upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
for (size_t i = 0; i < count; i++) {
memcpy(UPB_PTR_AT(in, in->unknown_end, char), data[i].data, data[i].size);
in->unknown_end += data[i].size;
}
// TODO: b/376969853 - Add debug check that the unknown field is an overall
// valid proto field
return true;
}

void _upb_Message_DiscardUnknown_shallow(upb_Message* msg) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
Expand Down
46 changes: 25 additions & 21 deletions upb/wire/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,18 +376,6 @@ static char* upb_Decoder_EncodeVarint32(uint32_t val, char* ptr) {
return ptr;
}

static void _upb_Decoder_AddUnknownVarints(upb_Decoder* d, upb_Message* msg,
uint32_t val1, uint32_t val2) {
char buf[20];
char* end = buf;
end = upb_Decoder_EncodeVarint32(val1, end);
end = upb_Decoder_EncodeVarint32(val2, end);

if (!UPB_PRIVATE(_upb_Message_AddUnknown)(msg, buf, end - buf, &d->arena)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
}

UPB_FORCEINLINE
bool _upb_Decoder_CheckEnum(upb_Decoder* d, const char* ptr, upb_Message* msg,
const upb_MiniTableEnum* e,
Expand All @@ -404,7 +392,15 @@ bool _upb_Decoder_CheckEnum(upb_Decoder* d, const char* ptr, upb_Message* msg,
upb_Message* unknown_msg =
field->UPB_PRIVATE(mode) & kUpb_LabelFlags_IsExtension ? d->unknown_msg
: msg;
_upb_Decoder_AddUnknownVarints(d, unknown_msg, tag, v);
char buf[20];
char* end = buf;
end = upb_Decoder_EncodeVarint32(tag, end);
end = upb_Decoder_EncodeVarint32(v, end);

if (!UPB_PRIVATE(_upb_Message_AddUnknown)(unknown_msg, buf, end - buf,
&d->arena)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
return false;
}

Expand Down Expand Up @@ -675,8 +671,16 @@ static const char* _upb_Decoder_DecodeToMap(
if (status != kUpb_EncodeStatus_Ok) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
_upb_Decoder_AddUnknownVarints(d, msg, tag, size);
if (!UPB_PRIVATE(_upb_Message_AddUnknown)(msg, buf, size, &d->arena)) {
char delim_buf[20];
char* delim_end = delim_buf;
delim_end = upb_Decoder_EncodeVarint32(tag, delim_end);
delim_end = upb_Decoder_EncodeVarint32(size, delim_end);
upb_StringView unknown[] = {
{delim_buf, delim_end - delim_buf},
{buf, size},
};

if (!UPB_PRIVATE(_upb_Message_AddUnknownV)(msg, &d->arena, unknown, 2)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
} else {
Expand Down Expand Up @@ -846,12 +850,12 @@ static void upb_Decoder_AddUnknownMessageSetItem(upb_Decoder* d,

ptr = upb_Decoder_EncodeVarint32(kEndItemTag, ptr);
char* end = ptr;

if (!UPB_PRIVATE(_upb_Message_AddUnknown)(msg, buf, split - buf, &d->arena) ||
!UPB_PRIVATE(_upb_Message_AddUnknown)(msg, message_data, message_size,
&d->arena) ||
!UPB_PRIVATE(_upb_Message_AddUnknown)(msg, split, end - split,
&d->arena)) {
upb_StringView unknown[] = {
{buf, split - buf},
{message_data, message_size},
{split, end - split},
};
if (!UPB_PRIVATE(_upb_Message_AddUnknownV)(msg, &d->arena, unknown, 3)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
}
Expand Down

0 comments on commit 1863e58

Please sign in to comment.