From 60ed595087d37d3f56194b601af73dfd5edf6c01 Mon Sep 17 00:00:00 2001 From: masesdevelopers <94312179+masesdevelopers@users.noreply.github.com> Date: Wed, 26 Jun 2024 02:22:27 +0200 Subject: [PATCH] Added check on length in deserialization of byte arrays --- src/documentation/articles/serialization.md | 4 ++-- src/net/KEFCore.SerDes.Avro/AvroKEFCoreSerDes.cs | 6 ++++++ src/net/KEFCore.SerDes.Protobuf/ProtobufKEFCoreSerDes.cs | 4 ++-- src/net/KEFCore.SerDes/DefaultKEFCoreSerDes.cs | 4 ++-- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/documentation/articles/serialization.md b/src/documentation/articles/serialization.md index 8dfd6a4..f926636 100644 --- a/src/documentation/articles/serialization.md +++ b/src/documentation/articles/serialization.md @@ -176,7 +176,7 @@ public class CustomKeySerDes : SerDesRaw /// public override T DeserializeWithHeaders(string topic, Headers headers, byte[] data) { - if (data == null) return default; + if (data == null || data.Length == 0) return default; return System.Text.Json.JsonSerializer.Deserialize(data)!; } } @@ -230,7 +230,7 @@ public class CustomValueContainerSerDes : SerDesRaw /// public override T DeserializeWithHeaders(string topic, Headers headers, byte[] data) { - if (data == null) return default; + if (data == null || data.Length == 0) return default; return System.Text.Json.JsonSerializer.Deserialize(data)!; } } diff --git a/src/net/KEFCore.SerDes.Avro/AvroKEFCoreSerDes.cs b/src/net/KEFCore.SerDes.Avro/AvroKEFCoreSerDes.cs index 5b5660d..31e2626 100644 --- a/src/net/KEFCore.SerDes.Avro/AvroKEFCoreSerDes.cs +++ b/src/net/KEFCore.SerDes.Avro/AvroKEFCoreSerDes.cs @@ -152,6 +152,7 @@ public override TData Deserialize(string topic, byte[] data) public override TData DeserializeWithHeaders(string topic, Headers headers, byte[] data) { if (_defaultSerDes != null) return _defaultSerDes.DeserializeWithHeaders(topic, headers, data); + if (data == null || data.Length == 0) return default!; using MemoryStream memStream = new(data); BinaryDecoder decoder = new(memStream); @@ -329,6 +330,7 @@ public override TData Deserialize(string topic, byte[] data) public override TData DeserializeWithHeaders(string topic, Headers headers, byte[] data) { if (_defaultSerDes != null) return _defaultSerDes.DeserializeWithHeaders(topic, headers, data); + if (data == null || data.Length == 0) return default!; using MemoryStream memStream = new(data); JsonDecoder decoder = new(AvroKeyContainer._SCHEMA, memStream); @@ -508,6 +510,8 @@ public override TData Deserialize(string topic, byte[] data) /// public override TData DeserializeWithHeaders(string topic, Headers headers, byte[] data) { + if (data == null || data.Length == 0) return default!; + using MemoryStream memStream = new(data); BinaryDecoder decoder = new(memStream); TData t = (TData)Activator.CreateInstance(typeof(TData))!; @@ -681,6 +685,8 @@ public override TData Deserialize(string topic, byte[] data) /// public override TData DeserializeWithHeaders(string topic, Headers headers, byte[] data) { + if (data == null || data.Length == 0) return default!; + using MemoryStream memStream = new(data); JsonDecoder decoder = new(AvroValueContainer._SCHEMA, memStream); TData t = (TData)Activator.CreateInstance(typeof(TData))!; diff --git a/src/net/KEFCore.SerDes.Protobuf/ProtobufKEFCoreSerDes.cs b/src/net/KEFCore.SerDes.Protobuf/ProtobufKEFCoreSerDes.cs index 103dd90..93cdc47 100644 --- a/src/net/KEFCore.SerDes.Protobuf/ProtobufKEFCoreSerDes.cs +++ b/src/net/KEFCore.SerDes.Protobuf/ProtobufKEFCoreSerDes.cs @@ -142,7 +142,7 @@ public override TData DeserializeWithHeaders(string topic, Headers headers, byte { if (_defaultSerDes != null) return _defaultSerDes.DeserializeWithHeaders(topic, headers, data); - if (data == null) return default!; + if (data == null || data.Length == 0) return default!; KeyContainer container = KeyContainer.Parser.ParseFrom(data); @@ -315,7 +315,7 @@ public override TData Deserialize(string topic, byte[] data) /// public override TData DeserializeWithHeaders(string topic, Headers headers, byte[] data) { - if (data == null) return default!; + if (data == null || data.Length == 0) return default!; var container = Storage.ValueContainer.Parser.ParseFrom(data); return (Activator.CreateInstance(typeof(TData), container) as TData)!; } diff --git a/src/net/KEFCore.SerDes/DefaultKEFCoreSerDes.cs b/src/net/KEFCore.SerDes/DefaultKEFCoreSerDes.cs index d505479..7668cda 100644 --- a/src/net/KEFCore.SerDes/DefaultKEFCoreSerDes.cs +++ b/src/net/KEFCore.SerDes/DefaultKEFCoreSerDes.cs @@ -143,7 +143,7 @@ public override TData DeserializeWithHeaders(string topic, Headers headers, byte { if (_defaultSerDes != null) return _defaultSerDes.DeserializeWithHeaders(topic, headers, data); - if (data == null) return default!; + if (data == null || data.Length == 0) return default!; return System.Text.Json.JsonSerializer.Deserialize(data, _options)!; } } @@ -315,7 +315,7 @@ public override TData Deserialize(string topic, byte[] data) /// public override TData DeserializeWithHeaders(string topic, Headers headers, byte[] data) { - if (data == null) return default!; + if (data == null || data.Length == 0) return default!; return System.Text.Json.JsonSerializer.Deserialize(data, _options)!; } }