Skip to content

Commit c4e02f9

Browse files
Fix not returing safe arrays decoupled from netty's buffer
1 parent 0ae336d commit c4e02f9

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

Codon.BinaryCodec/BinaryCodecs.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void Write(IByteBuffer buffer, byte[] value)
143143
public byte[] Read(IByteBuffer buffer)
144144
{
145145
var size = BinaryCodec.VAR_INT.Read(buffer);
146-
return buffer.ReadBytes(size).Array;
146+
return buffer.ReadBytes(size).ToByteArraySafe(size);
147147
}
148148
}
149149

@@ -176,7 +176,7 @@ public string Read(IByteBuffer buffer)
176176
{
177177
var size = BinaryCodec.VAR_INT.Read(buffer);
178178
if (size < 0) throw new InvalidDataException("String cannot have negative length");
179-
var stringBytes = buffer.ReadBytes(size).Array;
179+
var stringBytes = buffer.ReadBytes(size).ToByteArraySafe();
180180
return Encoding.UTF8.GetString(stringBytes);
181181
}
182182
}
@@ -190,7 +190,7 @@ public void Write(IByteBuffer buffer, byte[] value)
190190

191191
public byte[] Read(IByteBuffer buffer)
192192
{
193-
return buffer.ReadBytes(buffer.Array.Length).Array;
193+
return buffer.ReadBytes(buffer.Array.Length).ToByteArraySafe();
194194
}
195195
}
196196

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2026 SynesthesiaDev <synesthesiadev@proton.me>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
using System;
5+
using DotNetty.Buffers;
6+
7+
namespace Codon.Binary;
8+
9+
public static class ByteBufferExtensions
10+
{
11+
public static byte[] ToByteArraySafe(this IByteBuffer buffer, int size)
12+
{
13+
var destination = new byte[size];
14+
buffer.ReadBytes(destination);
15+
return destination;
16+
}
17+
18+
public static byte[] ToByteArraySafe(this IByteBuffer buffer)
19+
{
20+
int size = Math.Max(0, buffer.ReadableBytes);
21+
return buffer.ToByteArraySafe(size);
22+
}
23+
24+
}

0 commit comments

Comments
 (0)