Skip to content

Commit

Permalink
Merge "protozero: Finalize nested message when appending bytes" into …
Browse files Browse the repository at this point in the history
…main
  • Loading branch information
skyostil authored and Gerrit Code Review committed Sep 12, 2023
2 parents d1efaa8 + a7b7bf1 commit aa74d0a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/protozero/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ void Message::AppendBytes(uint32_t field_id, const void* src, size_t size) {
size_t Message::AppendScatteredBytes(uint32_t field_id,
ContiguousMemoryRange* ranges,
size_t num_ranges) {
if (nested_message_)
EndNestedMessage();

size_t size = 0;
for (size_t i = 0; i < num_ranges; ++i) {
size += ranges[i].size();
Expand Down
22 changes: 22 additions & 0 deletions src/protozero/message_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,28 @@ TEST_F(MessageTest, AppendScatteredBytes) {
EXPECT_EQ("42424242", GetNextSerializedBytes(4));
}

TEST_F(MessageTest, AppendScatteredBytesFinalizesNestedMessage) {
Message* root_msg = NewMessage();

uint8_t buffer[42];
memset(buffer, 0x42, sizeof(buffer));

FakeChildMessage* nested_msg =
root_msg->BeginNestedMessage<FakeChildMessage>(9001 /* field_id */);
nested_msg->AppendVarInt(4 /* field_id */, 2);
uint8_t* nested_msg_size_field = nested_msg->size_field();

EXPECT_FALSE(nested_msg->is_finalized());
EXPECT_EQ(0u, *nested_msg_size_field);

ContiguousMemoryRange ranges[] = {{buffer, buffer + sizeof(buffer)}};
root_msg->AppendScatteredBytes(1 /* field_id */, ranges, 1);

// Nested message should have been finalized as a side effect of appending
// scattered bytes.
EXPECT_EQ(0x82u, *nested_msg_size_field);
}

// Checks that the size field of root and nested messages is properly written
// on finalization.
TEST_F(MessageTest, BackfillSizeOnFinalization) {
Expand Down

0 comments on commit aa74d0a

Please sign in to comment.