Skip to content

Commit

Permalink
pw_protobuf: Replace contents of strings and bytes when decoding
Browse files Browse the repository at this point in the history
An incorrect reading of the protobuf spec had the table decoder
appending subsequent strings and bytes fields to each other, not
replacing the contents.

Correct that.

Change-Id: I826aeff12dd6038c7b0fdc596203d4c195bd5b19
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/93501
Pigweed-Auto-Submit: Scott James Remnant <[email protected]>
Reviewed-by: Keir Mierle <[email protected]>
Commit-Queue: Auto-Submit <[email protected]>
  • Loading branch information
keybuk authored and CQ Bot Account committed May 3, 2022
1 parent 8731e91 commit be0fe60
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
27 changes: 27 additions & 0 deletions pw_protobuf/codegen_message_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,33 @@ TEST(CodegenMessage, ReadStringCallback) {
ASSERT_EQ(status, OkStatus());
}

TEST(CodegenMessage, ReadMultipleString) {
// clang-format off
constexpr uint8_t proto_data[] = {
// pigweed.error_message
0x2a, 0x10, 'n', 'o', 't', ' ', 'a', ' ',
't', 'y', 'p', 'e', 'w', 'r', 'i', 't', 'e', 'r',
// pigweed.error_message
0x02a, 0x07, 'o', 'n', ' ', 'f', 'i', 'r', 'e'
};
// clang-format on

stream::MemoryReader reader(std::as_bytes(std::span(proto_data)));
Pigweed::StreamDecoder pigweed(reader);

Pigweed::Message message{};
const auto status = pigweed.Read(message);
ASSERT_EQ(status, OkStatus());

constexpr std::string_view kExpectedErrorMessage{"on fire"};

EXPECT_EQ(message.error_message.size(), kExpectedErrorMessage.size());
EXPECT_EQ(std::memcmp(message.error_message.data(),
kExpectedErrorMessage.data(),
kExpectedErrorMessage.size()),
0);
}

TEST(CodegenMessage, ReadRepeatedStrings) {
// clang-format off
constexpr uint8_t proto_data[] = {
Expand Down
9 changes: 4 additions & 5 deletions pw_protobuf/stream_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -636,14 +636,13 @@ Status StreamDecoder::Read(std::span<std::byte> message,
PW_CHECK(field->elem_size() == sizeof(std::byte),
"Mismatched message field type and size");
auto* vector = reinterpret_cast<pw::Vector<std::byte>*>(out.data());
if (vector->full()) {
if (vector->capacity() < delimited_field_size_) {
return Status::ResourceExhausted();
}
const size_t old_size = vector->size();
vector->resize(vector->capacity());
const auto sws = ReadDelimitedField(
std::span(vector->data(), vector->size()).subspan(old_size));
vector->resize(old_size + sws.size());
const auto sws =
ReadDelimitedField(std::span(vector->data(), vector->size()));
vector->resize(sws.size());
PW_TRY(sws);
}
break;
Expand Down

0 comments on commit be0fe60

Please sign in to comment.