google / google/protobuf.dart

Handling of large integers in `bytes` fields is inconsistent between serializers

Open
#731 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
572
Forks
196
Avg merge
1h 59m
Merged PRs (30d)
2

Description

Originally reported in https://github.com/google/protobuf.dart/issues/219#issuecomment-1211762838

If I have a `bytes` field and set the value something like `[999999]` (i.e. an integer list with integers larger than max value of a byte), binary serializer silently truncates the integer value, but JSON map and proto3 JSON serializers throw an exception.

Exception thrown by JSON map encoder:

```
Invalid argument (Not a byte value at index 0: 0x1869f): Instance(length:1) of '_GrowableList'
#0 _Base64Encoder.encodeChunk (dart:convert/base64.dart:374:5)
#1 _Base64Encoder.encode (dart:convert/base64.dart:328:9)
#2 Base64Encoder.convert (dart:convert/base64.dart:243:26)
#3 Codec.encode (dart:convert/codec.dart:21:32)
#4 base64Encode (dart:convert/base64.dart:41:48)
#5 _writeToJsonMap.convertToMap (package:protobuf/src/protobuf/json.dart:40:16)
#6 _writeToJsonMap (package:protobuf/src/protobuf/json.dart:79:33)
#7 GeneratedMessage.writeToJsonMap (package:protobuf/src/protobuf/generated_message.dart:203:44)
#8 GeneratedMessage.writeToJson (package:protobuf/src/protobuf/generated_message.dart:218:38)
```

Exception thrown by proto3 JSON encoder:

```
Unhandled exception:
Invalid argument (Not a byte value at index 0: 0x1869f): Instance(length:1) of '_GrowableList'
#0 _Base64Encoder.encodeChunk (dart:convert/base64.dart:374:5)
#1 _Base64Encoder.encode (dart:convert/base64.dart:328:9)
#2 Base64Encoder.convert (dart:convert/base64.dart:243:26)
#3 Codec.encode (dart:convert/codec.dart:21:32)
#4 base64Encode (dart:convert/base64.dart:41:48)
#5 _writeToProto3Json.valueToProto3Json (package:protobuf/src/protobuf/proto3_json.dart:77:18)
#6 _writeToProto3Json (package:protobuf/src/protobuf/proto3_json.dart:108:19)
#7 GeneratedMessage.toProto3Json (package:protobuf/src/protobuf/generated_message.dart:235:7)
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with a bytes field containing an integer such as 999999, then inspect package/protobuf/src/protobuf/json.dart and package/protobuf/src/protobuf/proto3_json.dart alongside the binary serializer. Compare the existing serializer tests and determine the expected handling for out-of-range integers. Done means the serializers behave consistently and the regression is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.