Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid holding onto the buffer when parsing unknown length-delimited fields #863

Merged
merged 5 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions protobuf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## 3.0.1-dev

* Avoid holding onto the input buffer when parsing unknown length-delimited
osa1 marked this conversation as resolved.
Show resolved Hide resolved
fields. ([#863])

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

## 3.0.0

* Require Dart `2.19`.
Expand Down
8 changes: 7 additions & 1 deletion protobuf/lib/src/protobuf/coded_buffer_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ class CodedBufferReader {
}

bool readBool() => _readRawVarint32(true) != 0;
List<int> readBytes() {

/// Read a length-delimited field as bytes. Note that the returned
/// [Uint8List] will be a view of the [CodedBufferReader]'s buffer. When
/// 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() {
osa1 marked this conversation as resolved.
Show resolved Hide resolved
final length = readInt32();
_checkLimit(length);
return Uint8List.view(
Expand Down
3 changes: 2 additions & 1 deletion protobuf/lib/src/protobuf/unknown_field_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class UnknownFieldSet {
mergeFixed64Field(number, input.readFixed64());
return true;
case WIRETYPE_LENGTH_DELIMITED:
mergeLengthDelimitedField(number, input.readBytes());
mergeLengthDelimitedField(
number, Uint8List.fromList(input.readBytes()));
return true;
case WIRETYPE_START_GROUP:
final subGroup = input.readUnknownFieldSetGroup(number);
Expand Down
27 changes: 27 additions & 0 deletions protoc_plugin/test/unknown_field_set_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:typed_data';

import 'package:protobuf/protobuf.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -326,4 +328,29 @@ void main() {
final m2 = TestAllExtensions()..unknownFields;
expect(m.hashCode, m2.hashCode);
});

test('Copy length delimited fields', () {
// Length-delimited fields should be copied before adding to the unknown
// field set to avoid aliasing.
final originalBytes = [1, 2, 3, 4, 5, 6];
final bytes = Uint8List.fromList([
10, // tag = 1, type = length delimited
originalBytes.length,
...originalBytes
]);

final parsed = UnknownFieldSet()
..mergeFromCodedBufferReader(CodedBufferReader(bytes));

expect(parsed.getField(1)?.lengthDelimited, [originalBytes]);

// Modify the message. Input buffer should not be updated.
final newBytes = [9, 8, 7, 6, 5, 4];
parsed.getField(1)!.lengthDelimited[0].setRange(0, 6, newBytes);
expect(bytes.sublist(2), originalBytes);

// Modify the input buffer. Message should not be updated.
bytes.setRange(2, 8, [10, 11, 12, 13, 14, 15]);
expect(parsed.getField(1)!.lengthDelimited[0], newBytes);
});
}