Skip to content

Commit 459e2bd

Browse files
authored
Merge pull request MessagePack-CSharp#1041 from pCYSl5EDgo/ZeroLengthArrayDeserialization
Use `Array.Empty<T>()` instead of `new T[]` wherever possible
2 parents ebdb29c + ee434cd commit 459e2bd

File tree

16 files changed

+616
-485
lines changed

16 files changed

+616
-485
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ using (var ms = new MemoryStream())
791791
using (var ms = new MemoryStream())
792792
{
793793
// serialize empty array.
794-
ProtoBuf.Serializer.Serialize<Parent>(ms, new Parent { Array = new int[0] });
794+
ProtoBuf.Serializer.Serialize<Parent>(ms, new Parent { Array = System.Array.Empty<int>() });
795795

796796
ms.Position = 0;
797797
var result = ProtoBuf.Serializer.Deserialize<Parent>(ms);

src/MessagePack.ImmutableCollection/Formatters.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ protected override ImmutableStack<T> Complete(T[] intermediateCollection)
218218

219219
protected override T[] Create(int count, MessagePackSerializerOptions options)
220220
{
221-
return new T[count];
221+
return count == 0 ? Array.Empty<T>() : new T[count];
222222
}
223223
}
224224

@@ -308,7 +308,7 @@ protected override IImmutableStack<T> Complete(T[] intermediateCollection)
308308

309309
protected override T[] Create(int count, MessagePackSerializerOptions options)
310310
{
311-
return new T[count];
311+
return count == 0 ? Array.Empty<T>() : new T[count];
312312
}
313313
}
314314

src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/CollectionFormatter.cs

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,30 @@ public T[] Deserialize(ref MessagePackReader reader, MessagePackSerializerOption
4343
{
4444
return default;
4545
}
46-
else
46+
47+
var len = reader.ReadArrayHeader();
48+
if (len == 0)
4749
{
48-
IMessagePackFormatter<T> formatter = options.Resolver.GetFormatterWithVerify<T>();
50+
return Array.Empty<T>();
51+
}
4952

50-
var len = reader.ReadArrayHeader();
51-
var array = new T[len];
52-
options.Security.DepthStep(ref reader);
53-
try
54-
{
55-
for (int i = 0; i < array.Length; i++)
56-
{
57-
reader.CancellationToken.ThrowIfCancellationRequested();
58-
array[i] = formatter.Deserialize(ref reader, options);
59-
}
60-
}
61-
finally
53+
IMessagePackFormatter<T> formatter = options.Resolver.GetFormatterWithVerify<T>();
54+
var array = new T[len];
55+
options.Security.DepthStep(ref reader);
56+
try
57+
{
58+
for (int i = 0; i < array.Length; i++)
6259
{
63-
reader.Depth--;
60+
reader.CancellationToken.ThrowIfCancellationRequested();
61+
array[i] = formatter.Deserialize(ref reader, options);
6462
}
65-
66-
return array;
6763
}
64+
finally
65+
{
66+
reader.Depth--;
67+
}
68+
69+
return array;
6870
}
6971
}
7072

@@ -417,7 +419,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria
417419

418420
protected override T[] Create(int count, MessagePackSerializerOptions options)
419421
{
420-
return new T[count];
422+
return count == 0 ? Array.Empty<T>() : new T[count];
421423
}
422424

423425
protected override Stack<T>.Enumerator GetSourceEnumerator(Stack<T> source)
@@ -473,7 +475,7 @@ protected override ReadOnlyCollection<T> Complete(T[] intermediateCollection)
473475

474476
protected override T[] Create(int count, MessagePackSerializerOptions options)
475477
{
476-
return new T[count];
478+
return count == 0 ? Array.Empty<T>() : new T[count];
477479
}
478480
}
479481

@@ -487,7 +489,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria
487489

488490
protected override T[] Create(int count, MessagePackSerializerOptions options)
489491
{
490-
return new T[count];
492+
return count == 0 ? Array.Empty<T>() : new T[count];
491493
}
492494

493495
protected override IList<T> Complete(T[] intermediateCollection)
@@ -506,7 +508,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria
506508

507509
protected override T[] Create(int count, MessagePackSerializerOptions options)
508510
{
509-
return new T[count];
511+
return count == 0 ? Array.Empty<T>() : new T[count];
510512
}
511513

512514
protected override ICollection<T> Complete(T[] intermediateCollection)
@@ -560,7 +562,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria
560562

561563
protected override T[] Create(int count, MessagePackSerializerOptions options)
562564
{
563-
return new T[count];
565+
return count == 0 ? Array.Empty<T>() : new T[count];
564566
}
565567

566568
protected override IEnumerable<T> Complete(T[] intermediateCollection)
@@ -791,9 +793,13 @@ public IList Deserialize(ref MessagePackReader reader, MessagePackSerializerOpti
791793
return default(IList);
792794
}
793795

794-
IMessagePackFormatter<object> formatter = options.Resolver.GetFormatterWithVerify<object>();
795-
796796
var count = reader.ReadArrayHeader();
797+
if (count == 0)
798+
{
799+
return Array.Empty<object>();
800+
}
801+
802+
IMessagePackFormatter<object> formatter = options.Resolver.GetFormatterWithVerify<object>();
797803

798804
var list = new object[count];
799805
options.Security.DepthStep(ref reader);
@@ -967,7 +973,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria
967973

968974
protected override T[] Create(int count, MessagePackSerializerOptions options)
969975
{
970-
return new T[count];
976+
return count == 0 ? Array.Empty<T>() : new T[count];
971977
}
972978

973979
protected override IReadOnlyList<T> Complete(T[] intermediateCollection)
@@ -985,7 +991,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria
985991

986992
protected override T[] Create(int count, MessagePackSerializerOptions options)
987993
{
988-
return new T[count];
994+
return count == 0 ? Array.Empty<T>() : new T[count];
989995
}
990996

991997
protected override IReadOnlyCollection<T> Complete(T[] intermediateCollection)
@@ -1063,7 +1069,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria
10631069

10641070
protected override T[] Create(int count, MessagePackSerializerOptions options)
10651071
{
1066-
return new T[count];
1072+
return count == 0 ? Array.Empty<T>() : new T[count];
10671073
}
10681074

10691075
protected override ConcurrentStack<T> Complete(T[] intermediateCollection)

src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/DateTimeFormatters.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,21 @@ public DateTime[] Deserialize(ref MessagePackReader reader, MessagePackSerialize
5353
{
5454
return null;
5555
}
56-
else
56+
57+
var len = reader.ReadArrayHeader();
58+
if (len == 0)
5759
{
58-
var len = reader.ReadArrayHeader();
59-
var array = new DateTime[len];
60-
for (int i = 0; i < array.Length; i++)
61-
{
62-
var dateData = reader.ReadInt64();
63-
array[i] = DateTime.FromBinary(dateData);
64-
}
60+
return Array.Empty<DateTime>();
61+
}
6562

66-
return array;
63+
var array = new DateTime[len];
64+
for (int i = 0; i < array.Length; i++)
65+
{
66+
var dateData = reader.ReadInt64();
67+
array[i] = DateTime.FromBinary(dateData);
6768
}
69+
70+
return array;
6871
}
6972
}
7073
}

src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/PrimitiveObjectFormatter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ public object Deserialize(ref MessagePackReader reader, MessagePackSerializerOpt
281281
case MessagePackType.Array:
282282
{
283283
var length = reader.ReadArrayHeader();
284+
if (length == 0)
285+
{
286+
return Array.Empty<object>();
287+
}
284288

285289
IMessagePackFormatter<object> objectFormatter = resolver.GetFormatter<object>();
286290
var array = new object[length];

src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/StandardClassLibraryFormatter.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,20 @@ public String[] Deserialize(ref MessagePackReader reader, MessagePackSerializerO
8484
{
8585
return null;
8686
}
87-
else
87+
88+
var len = reader.ReadArrayHeader();
89+
if (len == 0)
8890
{
89-
var len = reader.ReadArrayHeader();
90-
var array = new String[len];
91-
for (int i = 0; i < array.Length; i++)
92-
{
93-
array[i] = reader.ReadString();
94-
}
91+
return Array.Empty<String>();
92+
}
9593

96-
return array;
94+
var array = new String[len];
95+
for (int i = 0; i < array.Length; i++)
96+
{
97+
array[i] = reader.ReadString();
9798
}
99+
100+
return array;
98101
}
99102
}
100103

src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Internal/AutomataDictionary.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,6 @@ public void EmitMatch(ILGenerator il, LocalBuilder bytesSpan, LocalBuilder key,
142142

143143
private class AutomataNode : IComparable<AutomataNode>
144144
{
145-
private static readonly AutomataNode[] EmptyNodes = new AutomataNode[0];
146-
private static readonly ulong[] EmptyKeys = new ulong[0];
147-
148145
#pragma warning disable SA1401 // Fields should be private
149146
internal ulong Key;
150147
internal int Value;
@@ -164,8 +161,8 @@ public AutomataNode(ulong key)
164161
{
165162
this.Key = key;
166163
this.Value = -1;
167-
this.nexts = EmptyNodes;
168-
this.nextKeys = EmptyKeys;
164+
this.nexts = Array.Empty<AutomataNode>();
165+
this.nextKeys = Array.Empty<ulong>();
169166
this.count = 0;
170167
this.OriginalKey = null;
171168
}

0 commit comments

Comments
 (0)