Skip to content

Commit

Permalink
Skip zero size containers in protobuf.
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalz800 committed Jan 15, 2022
1 parent cba44dc commit a3565e6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
19 changes: 19 additions & 0 deletions test/src/test_pb_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,4 +495,23 @@ TEST(test_pb_protocol, person_short)
EXPECT_EQ(data, new_data);
}

TEST(test_pb_protocol, default_person_in_address_book)
{
constexpr auto data = "\n\x00"_b;

address_book b;
zpp::bits::in{data, zpp::bits::no_size{}}(b).or_throw();

EXPECT_EQ(b.people.size(), 1u);
EXPECT_EQ(b.people[0].name, "");
EXPECT_EQ(b.people[0].id, 0);
EXPECT_EQ(b.people[0].email, "");
EXPECT_EQ(b.people[0].phones.size(), 0u);

std::array<std::byte, "0a021000"_decode_hex.size()> new_data;
zpp::bits::out{new_data, zpp::bits::no_size{}}(b).or_throw();

EXPECT_EQ(new_data, "0a021000"_decode_hex);
}

} // namespace test_pb_protocol
19 changes: 14 additions & 5 deletions zpp_bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -4590,11 +4590,14 @@ struct pb
std::byte>;
}) {
constexpr auto tag = make_tag<tag_type, Index>();
if (auto result =
archive(tag,
varint{item.size() *
sizeof(typename type::value_type)},
unsized(item));
auto size = item.size();
if (!size) [[unlikely]] {
return {};
}
if (auto result = archive(
tag,
varint{size * sizeof(typename type::value_type)},
unsized(item));
failure(result)) [[unlikely]] {
return result;
}
Expand All @@ -4610,6 +4613,9 @@ struct pb
size +=
varint_size<type::value_type::encoding>(element.value);
}
if (!size) [[unlikely]] {
return {};
}
if (auto result = archive(tag, varint{size}, unsized(item));
failure(result)) [[unlikely]] {
return result;
Expand All @@ -4626,6 +4632,9 @@ struct pb
for (auto & element : item) {
size += varint_size(std::underlying_type_t<type>(element));
}
if (!size) [[unlikely]] {
return {};
}
if (auto result = archive(tag, varint{size}); failure(result))
[[unlikely]] {
return result;
Expand Down

0 comments on commit a3565e6

Please sign in to comment.