Skip to content

Commit

Permalink
pw_protobuf: Skip unknown fields when struct decoding
Browse files Browse the repository at this point in the history
Protobuf parsers are expected to skip unknown fields, and it turns out
in practice encoders sometimes skip extra fields of their own in using
this as a guarantee.

Change StreamDecoder::Read to skip unknown fields, and file a TODO bug
to figure out how to allow them to be inspected and serialized back out
again later.

Change-Id: If1d74174522178bc13e64162dc67c7c2e8b8c713
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/92020
Pigweed-Auto-Submit: Scott James Remnant <[email protected]>
Reviewed-by: Armando Montanez <[email protected]>
Commit-Queue: Auto-Submit <[email protected]>
  • Loading branch information
keybuk authored and CQ Bot Account committed Apr 26, 2022
1 parent bc8b985 commit 59547bd
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions pw_protobuf/stream_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -515,13 +515,15 @@ Status StreamDecoder::Read(std::span<std::byte> message,
PW_TRY(status_);

while (Next().ok()) {
// If the field is not found in the table, immediately return NotFound()
// without advancing the cursor to allow the caller to inspect the field.
// Find the field in the table,
// TODO(pwbug/650): Finding the field can be made more efficient.
const auto field =
std::find(table.begin(), table.end(), current_field_.field_number());
if (field == table.end()) {
return Status::NotFound();
// If the field is not found, skip to the next one.
// TODO(pwbug/659): Provide a way to allow the caller to inspect unknown
// fields, and serialize them back out later.
continue;
}

// Calculate the span of bytes corresponding to the structure field to
Expand Down

0 comments on commit 59547bd

Please sign in to comment.