Fix a potential issue in CodedBufferWriter (#594)

Currently `CodedBufferWriter`, when serializing a field, manually masks
the "repeated", "required", and "packed" bits to get the "base type" of
the field.

However it also needs to mask the "map" bit as the function to map the
"base type" to an array index (`_valueTypeIndex`) only works for values
that are a power of 2, i.e. there needs to be at most one bit set.

This code still works today because of the special case when the map bit
set, after getting the base type incorrectly.

In other words, when the map bit is set we compute the wire type
incorrectly, but we don't use the incorrect value.

Changed in this PR:

1. `_valueTypeIndex` now has an assertion checking that the argument is
   really a power of 2.

2. `valueType` is now computed using `PbFieldType._baseType`, which
   correctly masks the "map" bit.

3. `wireFormat` is moved after the special case that checks for the map
   bit.

With (1) tests in protoc_compiler start to fail. Either one of (2) or
(3) fixes the issue, but I think both are improvements.

JSON serializers correctly use the `PbFieldType._baseType` function to
get the base type so no changes needed.
1 file changed
tree: 1acbec915712f42580542e0d76d9035867689c44
  1. .github/
  2. api_benchmark/
  3. protobuf/
  4. protoc_plugin/
  5. query_benchmark/
  6. tool/
  7. .gitignore
  8. analysis_options.yaml
  9. mono_repo.yaml
  10. README.md
README.md

Protobuf support for Dart

CI status pub package

Protocol Buffers (protobuf) are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data.

This repository is home to packages related to protobuf for Dart. It contains: