Skip to content

Commit

Permalink
Rename readBytes -> readBytesAsView, bump version number
Browse files Browse the repository at this point in the history
  • Loading branch information
osa1 committed Jul 31, 2023
1 parent 63d5d20 commit 08a4a99
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 17 deletions.
5 changes: 3 additions & 2 deletions protobuf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## 3.0.1-dev
## 4.0.0-dev

* Avoid holding onto the input buffer when parsing unknown length-delimited
fields. ([#863])
fields. `CodedBufferReader` `readBytes` is renamed as `readBytesAsView`.
([#863])

[#863]: https://github.com/google/protobuf.dart/pull/863

Expand Down
5 changes: 3 additions & 2 deletions protobuf/lib/src/protobuf/coded_buffer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ void _mergeFromCodedBufferReader(BuilderInfo meta, _FieldSet fs,
fs._setFieldUnchecked(meta, fi, input.readBool());
break;
case PbFieldType._OPTIONAL_BYTES:
fs._setFieldUnchecked(meta, fi, Uint8List.fromList(input.readBytes()));
fs._setFieldUnchecked(
meta, fi, Uint8List.fromList(input.readBytesAsView()));
break;
case PbFieldType._OPTIONAL_STRING:
fs._setFieldUnchecked(meta, fi, input.readString());
Expand Down Expand Up @@ -133,7 +134,7 @@ void _mergeFromCodedBufferReader(BuilderInfo meta, _FieldSet fs,
case PbFieldType._REPEATED_BYTES:
fs
._ensureRepeatedField(meta, fi)
.add(Uint8List.fromList(input.readBytes()));
.add(Uint8List.fromList(input.readBytesAsView()));
break;
case PbFieldType._REPEATED_STRING:
fs._ensureRepeatedField(meta, fi).add(input.readString());
Expand Down
6 changes: 3 additions & 3 deletions protobuf/lib/src/protobuf/coded_buffer_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ class CodedBufferReader {
/// storing the returned value directly (instead of e.g. parsing it as a
/// UTF-8 string and copying) make sure to copy it to avoid holding on to the
/// whole message.
Uint8List readBytes() {
Uint8List readBytesAsView() {
final length = readInt32();
_checkLimit(length);
return Uint8List.view(
_buffer.buffer, _buffer.offsetInBytes + _bufferPos - length, length);
}

String readString() => _utf8.decode(readBytes());
String readString() => _utf8.decode(readBytesAsView());
double readFloat() => _readByteData(4).getFloat32(0, Endian.little);
double readDouble() => _readByteData(8).getFloat64(0, Endian.little);

Expand Down Expand Up @@ -178,7 +178,7 @@ class CodedBufferReader {
readFixed64();
return true;
case WIRETYPE_LENGTH_DELIMITED:
readBytes();
readBytesAsView();
return true;
case WIRETYPE_FIXED32:
readFixed32();
Expand Down
12 changes: 7 additions & 5 deletions protobuf/lib/src/protobuf/message_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ abstract class $_MessageSet extends GeneratedMessage {
//
// We can see the fields in any order, so loop until parsing both fields.
int? typeId;
List<int>? message;
Uint8List? message;
while (true) {
final tag = input.readTag();
final tagNumber = getTagFieldNumber(tag);
Expand All @@ -97,7 +97,7 @@ abstract class $_MessageSet extends GeneratedMessage {
message = null;
}
} else if (tagNumber == _messageSetItemMessageTag) {
message = input.readBytes();
message = input.readBytesAsView();
if (typeId != null) {
_parseExtension(typeId, message, extensionRegistry);
typeId = null;
Expand All @@ -121,15 +121,17 @@ abstract class $_MessageSet extends GeneratedMessage {
}

void _parseExtension(
int typeId, List<int> message, ExtensionRegistry extensionRegistry) {
int typeId, Uint8List message, ExtensionRegistry extensionRegistry) {
final ext =
extensionRegistry.getExtension(info_.qualifiedMessageName, typeId);
if (ext == null) {
final messageItem = UnknownFieldSet();
messageItem.addField(_messageSetItemTypeIdTag,
UnknownFieldSetField()..varints.add(Int64(typeId)));
messageItem.addField(_messageSetItemMessageTag,
UnknownFieldSetField()..lengthDelimited.add(message));
messageItem.addField(
_messageSetItemMessageTag,
UnknownFieldSetField()
..lengthDelimited.add(Uint8List.fromList(message)));

final itemListField =
_fieldSet._ensureUnknownFields().getField(_messageSetItemsTag) ??
Expand Down
2 changes: 1 addition & 1 deletion protobuf/lib/src/protobuf/unknown_field_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class UnknownFieldSet {
return true;
case WIRETYPE_LENGTH_DELIMITED:
mergeLengthDelimitedField(
number, Uint8List.fromList(input.readBytes()));
number, Uint8List.fromList(input.readBytesAsView()));
return true;
case WIRETYPE_START_GROUP:
final subGroup = input.readUnknownFieldSetGroup(number);
Expand Down
2 changes: 1 addition & 1 deletion protobuf/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: protobuf
version: 3.0.1-dev
version: 4.0.0-dev
description: >-
Runtime library for protocol buffers support. Use with package:protoc_plugin
to generate dart code for your '.proto' files.
Expand Down
4 changes: 2 additions & 2 deletions protobuf/test/coded_buffer_reader_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void main() {
expect(cis.readString(), 'optional_string');

expect(cis.readTag(), makeTag(115, WIRETYPE_LENGTH_DELIMITED));
expect(cis.readBytes(), 'optional_bytes'.codeUnits);
expect(cis.readBytesAsView(), 'optional_bytes'.codeUnits);
}

test('normal-list', () {
Expand Down Expand Up @@ -113,7 +113,7 @@ void main() {
final input = CodedBufferReader(output.toBuffer());
expect(input.readTag(), tag);

expect(input.readBytes, throwsInvalidProtocolBufferException);
expect(input.readBytesAsView, throwsInvalidProtocolBufferException);
});

/// Tests that if we read a string that contains invalid UTF-8, no exception
Expand Down
2 changes: 1 addition & 1 deletion protoc_plugin/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:
dependencies:
fixnum: ^1.0.0
path: ^1.8.0
protobuf: ^3.0.0
protobuf: ^4.0.0

dev_dependencies:
collection: ^1.15.0
Expand Down

0 comments on commit 08a4a99

Please sign in to comment.