Skip to content

Commit 890446f

Browse files
Sync changes from internal repo. (#91)
* Fix hashing in PbList. Fixes #87
1 parent 72dc332 commit 890446f

File tree

6 files changed

+30
-27
lines changed

6 files changed

+30
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.children
2+
.dart_tool/
23
.idea
34
.packages
45
.pub

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.7.2
2+
3+
* Fix hashing for PbList.
4+
15
## 0.7.1
26

37
* Fix type in PbList.fold() for Dart 2.

lib/src/protobuf/field_set.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ class _FieldSet {
413413

414414
void hashEnumList(PbList enums) {
415415
for (ProtobufEnum enm in enums) {
416-
hash = (31 * hash + enm.value) & 0x3fffffff;
416+
hash = 0x1fffffff & ((31 * hash) + enm.value);
417417
}
418418
}
419419

@@ -422,15 +422,14 @@ class _FieldSet {
422422
if (value is List && value.isEmpty) {
423423
return; // It's either repeated or an empty byte array.
424424
}
425-
hash = ((37 * hash) + fi.tagNumber) & 0x3fffffff;
425+
hash = 0x1fffffff & ((37 * hash) + fi.tagNumber);
426426
if (!_isEnum(fi.type)) {
427-
// TODO(sgjesse): Remove 'as Object' here when issue 14951 is fixed.
428-
hash = ((53 * hash) + (value as Object).hashCode) & 0x3fffffff;
427+
hash = 0x1fffffff & ((53 * hash) + value.hashCode);
429428
} else if (fi.isRepeated) {
430429
hashEnumList(value);
431430
} else {
432431
ProtobufEnum enm = value;
433-
hash = ((53 * hash) + enm.value) & 0x3fffffff;
432+
hash = 0x1fffffff & ((53 * hash) + enm.value);
434433
}
435434
}
436435

@@ -449,12 +448,12 @@ class _FieldSet {
449448
// Generate hash.
450449
hash = 41;
451450
// Hash with descriptor.
452-
hash = ((19 * hash) + _meta.hashCode) & 0x3fffffff;
451+
hash = 0x1fffffff & ((19 * hash) + _meta.hashCode);
453452
// Hash with fields.
454453
hashEachField();
455454
// Hash with unknown fields.
456455
if (hasUnknownFields) {
457-
hash = ((29 * hash) + _unknownFields.hashCode) & 0x3fffffff;
456+
hash = 0x1fffffff & ((29 * hash) + _unknownFields.hashCode);
458457
}
459458
return hash;
460459
}

lib/src/protobuf/pb_list.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ class PbList<E> extends ListBase<E> {
2727
int get hashCode {
2828
int hash = 0;
2929
for (final value in _wrappedList) {
30-
hash = (hash + value.hashCode) & 0x3fffffff;
31-
hash = (hash + hash << 10) & 0x3fffffff;
32-
hash = (hash ^ hash >> 6) & 0x3fffffff;
30+
hash = 0x1fffffff & (hash + value.hashCode);
31+
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
32+
hash = hash ^ (hash >> 6);
3333
}
34-
hash = (hash + hash << 3) & 0x3fffffff;
35-
hash = (hash ^ hash >> 11) & 0x3fffffff;
36-
hash = (hash + hash << 15) & 0x3fffffff;
37-
return hash;
34+
hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
35+
hash = hash ^ (hash >> 11);
36+
return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
3837
}
3938

4039
/// Returns an [Iterator] for the list.

lib/src/protobuf/unknown_field_set.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ class UnknownFieldSet {
125125
int get hashCode {
126126
int hash = 0;
127127
_fields.forEach((int number, Object value) {
128-
hash = ((37 * hash) + number) & 0x3fffffff;
129-
hash = ((53 * hash) + value.hashCode) & 0x3fffffff;
128+
hash = 0x1fffffff & ((37 * hash) + number);
129+
hash = 0x1fffffff & ((53 * hash) + value.hashCode);
130130
});
131131
return hash;
132132
}
@@ -193,25 +193,25 @@ class UnknownFieldSetField {
193193
int hash = 0;
194194
for (final value in lengthDelimited) {
195195
for (int i = 0; i < value.length; i++) {
196-
hash = (hash + value[i]) & 0x3fffffff;
197-
hash = (hash + hash << 10) & 0x3fffffff;
198-
hash = (hash ^ hash >> 6) & 0x3fffffff;
196+
hash = 0x1fffffff & (hash + value[i]);
197+
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
198+
hash = hash ^ (hash >> 6);
199199
}
200-
hash = (hash + hash << 3) & 0x3fffffff;
201-
hash = (hash ^ hash >> 11) & 0x3fffffff;
202-
hash = (hash + hash << 15) & 0x3fffffff;
200+
hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
201+
hash = hash ^ (hash >> 11);
202+
hash = 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
203203
}
204204
for (final value in varints) {
205-
hash = (hash + 7 * value.hashCode) & 0x3fffffff;
205+
hash = 0x1fffffff & (hash + (7 * value.hashCode));
206206
}
207207
for (final value in fixed32s) {
208-
hash = (hash + 37 * value.hashCode) & 0x3fffffff;
208+
hash = 0x1fffffff & (hash + (37 * value.hashCode));
209209
}
210210
for (final value in fixed64s) {
211-
hash = (hash + 53 * value.hashCode) & 0x3fffffff;
211+
hash = 0x1fffffff & (hash + (53 * value.hashCode));
212212
}
213213
for (final value in groups) {
214-
hash = (hash + value.hashCode) & 0x3fffffff;
214+
hash = 0x1fffffff & (hash + value.hashCode);
215215
}
216216
return hash;
217217
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: protobuf
2-
version: 0.7.1
2+
version: 0.7.2
33
author: Dart Team <misc@dartlang.org>
44
description: Runtime library for protocol buffers support.
55
homepage: https://github.com/dart-lang/protobuf

0 commit comments

Comments
 (0)