Skip to content

Commit

Permalink
upb: tag _upb_Message_AddUnknown() as UPB_PRIVATE()
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 590013675
  • Loading branch information
ericsalo authored and copybara-github committed Dec 12, 2023
1 parent a649dda commit 0c3e116
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 19 deletions.
1 change: 1 addition & 0 deletions upb/message/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ cc_test(
"//upb:base",
"//upb:mem",
"//upb:mini_table",
"//upb:port",
"//upb:wire",
"//upb/test:test_messages_proto2_upb_minitable",
"//upb/test:test_messages_proto2_upb_proto",
Expand Down
2 changes: 1 addition & 1 deletion upb/message/copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ upb_Message* _upb_Message_Copy(upb_Message* dst, const upb_Message* src,
if (unknown_size != 0) {
UPB_ASSERT(ptr);
// Make a copy into destination arena.
if (!_upb_Message_AddUnknown(dst, ptr, unknown_size, arena)) {
if (!UPB_PRIVATE(_upb_Message_AddUnknown)(dst, ptr, unknown_size, arena)) {
return NULL;
}
}
Expand Down
5 changes: 4 additions & 1 deletion upb/message/copy_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#include "upb/mini_table/message.h"
#include "upb/wire/encode.h"

// Must be last.
#include "upb/port/def.inc"

namespace {

// Proto2 test messages field numbers used for reflective access.
Expand Down Expand Up @@ -301,7 +304,7 @@ TEST(GeneratedCode, DeepCloneMessageWithUnknowns) {
ASSERT_EQ(status, kUpb_EncodeStatus_Ok);
std::string unknown_data(data, len);
// Add unknown data.
_upb_Message_AddUnknown(msg, data, len, source_arena);
UPB_PRIVATE(_upb_Message_AddUnknown)(msg, data, len, source_arena);
// Create clone.
upb_Arena* clone_arena = upb_Arena_New();
protobuf_test_messages_proto2_TestAllTypesProto2* clone =
Expand Down
4 changes: 2 additions & 2 deletions upb/message/internal/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ void _upb_Message_DiscardUnknown_shallow(upb_Message* msg);

// Adds unknown data (serialized protobuf data) to the given message.
// The data is copied into the message instance.
bool _upb_Message_AddUnknown(upb_Message* msg, const char* data, size_t len,
upb_Arena* arena);
bool UPB_PRIVATE(_upb_Message_AddUnknown)(upb_Message* msg, const char* data,
size_t len, upb_Arena* arena);

bool UPB_PRIVATE(_upb_Message_Realloc)(upb_Message* msg, size_t need,
upb_Arena* arena);
Expand Down
15 changes: 10 additions & 5 deletions upb/message/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@

#include "upb/message/message.h"

#include <stddef.h>
#include <stdint.h>
#include <string.h>

#include "upb/mem/arena.h"
#include "upb/message/internal/message.h"
#include "upb/message/internal/types.h"
#include "upb/mini_table/message.h"

// Must be last.
#include "upb/port/def.inc"

static const size_t message_overhead = sizeof(upb_Message_InternalData);

upb_Message* upb_Message_New(const upb_MiniTable* mini_table,
upb_Arena* arena) {
return _upb_Message_New(mini_table, arena);
upb_Message* upb_Message_New(const upb_MiniTable* m, upb_Arena* arena) {
return _upb_Message_New(m, arena);
}

bool _upb_Message_AddUnknown(upb_Message* msg, const char* data, size_t len,
upb_Arena* arena) {
bool UPB_PRIVATE(_upb_Message_AddUnknown)(upb_Message* msg, const char* data,
size_t len, upb_Arena* arena) {
if (!UPB_PRIVATE(_upb_Message_Realloc)(msg, len, arena)) return false;
upb_Message_Internal* in = upb_Message_Getinternal(msg);
memcpy(UPB_PTR_AT(in->internal, in->internal->unknown_end, char), data, len);
Expand Down
3 changes: 1 addition & 2 deletions upb/message/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ extern "C" {
#endif

// Creates a new message with the given mini_table on the given arena.
UPB_API upb_Message* upb_Message_New(const upb_MiniTable* mini_table,
upb_Arena* arena);
UPB_API upb_Message* upb_Message_New(const upb_MiniTable* m, upb_Arena* arena);

// Returns a reference to the message's unknown data.
const char* upb_Message_GetUnknown(const upb_Message* msg, size_t* len);
Expand Down
15 changes: 9 additions & 6 deletions upb/wire/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ static void _upb_Decoder_AddUnknownVarints(upb_Decoder* d, upb_Message* msg,
end = upb_Decoder_EncodeVarint32(val1, end);
end = upb_Decoder_EncodeVarint32(val2, end);

if (!_upb_Message_AddUnknown(msg, buf, end - buf, &d->arena)) {
if (!UPB_PRIVATE(_upb_Message_AddUnknown)(msg, buf, end - buf, &d->arena)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
}
Expand Down Expand Up @@ -660,7 +660,7 @@ static const char* _upb_Decoder_DecodeToMap(upb_Decoder* d, const char* ptr,
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
_upb_Decoder_AddUnknownVarints(d, msg, tag, size);
if (!_upb_Message_AddUnknown(msg, buf, size, &d->arena)) {
if (!UPB_PRIVATE(_upb_Message_AddUnknown)(msg, buf, size, &d->arena)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
} else {
Expand Down Expand Up @@ -838,9 +838,11 @@ static void upb_Decoder_AddUnknownMessageSetItem(upb_Decoder* d,
ptr = upb_Decoder_EncodeVarint32(kEndItemTag, ptr);
char* end = ptr;

if (!_upb_Message_AddUnknown(msg, buf, split - buf, &d->arena) ||
!_upb_Message_AddUnknown(msg, message_data, message_size, &d->arena) ||
!_upb_Message_AddUnknown(msg, split, end - split, &d->arena)) {
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_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
}
Expand Down Expand Up @@ -1225,7 +1227,8 @@ static const char* _upb_Decoder_DecodeUnknownField(upb_Decoder* d,
start = d->unknown;
d->unknown = NULL;
}
if (!_upb_Message_AddUnknown(msg, start, ptr - start, &d->arena)) {
if (!UPB_PRIVATE(_upb_Message_AddUnknown)(msg, start, ptr - start,
&d->arena)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
} else if (wire_type == kUpb_WireType_StartGroup) {
Expand Down
4 changes: 2 additions & 2 deletions upb/wire/internal/decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ UPB_INLINE const char* _upb_Decoder_BufferFlipCallback(
if (!old_end) _upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);

if (d->unknown) {
if (!_upb_Message_AddUnknown(d->unknown_msg, d->unknown,
old_end - d->unknown, &d->arena)) {
if (!UPB_PRIVATE(_upb_Message_AddUnknown)(
d->unknown_msg, d->unknown, old_end - d->unknown, &d->arena)) {
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
d->unknown = new_start;
Expand Down

0 comments on commit 0c3e116

Please sign in to comment.