From f284088ecfa0425e9ee2fc3fd6219c594ff45f1d Mon Sep 17 00:00:00 2001 From: pCYSl5EDgo Date: Mon, 14 Sep 2020 16:41:23 +0900 Subject: [PATCH] When deserialize length is 0, the return array should be Array.Empty(). --- .../Formatters/CollectionFormatter.cs | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/CollectionFormatter.cs b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/CollectionFormatter.cs index 550c7f312..7ca2afbbe 100644 --- a/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/CollectionFormatter.cs +++ b/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Formatters/CollectionFormatter.cs @@ -43,28 +43,30 @@ public T[] Deserialize(ref MessagePackReader reader, MessagePackSerializerOption { return default; } - else + + var len = reader.ReadArrayHeader(); + if (len == 0) { - IMessagePackFormatter formatter = options.Resolver.GetFormatterWithVerify(); + return Array.Empty(); + } - var len = reader.ReadArrayHeader(); - var array = new T[len]; - options.Security.DepthStep(ref reader); - try - { - for (int i = 0; i < array.Length; i++) - { - reader.CancellationToken.ThrowIfCancellationRequested(); - array[i] = formatter.Deserialize(ref reader, options); - } - } - finally + IMessagePackFormatter formatter = options.Resolver.GetFormatterWithVerify(); + var array = new T[len]; + options.Security.DepthStep(ref reader); + try + { + for (int i = 0; i < array.Length; i++) { - reader.Depth--; + reader.CancellationToken.ThrowIfCancellationRequested(); + array[i] = formatter.Deserialize(ref reader, options); } - - return array; } + finally + { + reader.Depth--; + } + + return array; } }