Skip to content

Commit 1088b41

Browse files
authored
fix: Creating a zero-length FastBufferReader no longer reports its size as 1 (backport) (#1724)
1 parent 40703a0 commit 1088b41

File tree

4 files changed

+59
-13
lines changed

4 files changed

+59
-13
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
2525
- Fixed error when serializing ConnectionApprovalMessage with scene management disabled when one or more objects is hidden via the CheckObjectVisibility delegate (#1720)
2626
- Fixed CheckObjectVisibility delegate not being properly invoked for connecting clients when Scene Management is enabled. (#1680)
2727
- Fixed NetworkList to properly call INetworkSerializable's NetworkSerialize() method (#1682)
28+
- Fixed FastBufferReader being created with a length of 1 if provided an input of length 0 (#1724)
2829
- Fixed The NetworkConfig's checksum hash includes the NetworkTick so that clients with a different tickrate than the server are identified and not allowed to connect (#1728)
2930
- Fixed OwnedObjects not being properly modified when using ChangeOwnership (#1731)
3031
- Improved performance in NetworkAnimator (#1735)

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ internal void __endSendClientRpc(ref FastBufferWriter bufferWriter, uint rpcMeth
208208
};
209209
clientRpcMessage.ReadBuffer = tempBuffer;
210210
clientRpcMessage.Handle(ref context);
211-
rpcWriteSize = tempBuffer.Length;
212211
}
213212

214213
bufferWriter.Dispose();

com.unity.netcode.gameobjects/Runtime/Serialization/FastBufferReader.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ internal unsafe void CommitBitwiseReads(int amount)
7676

7777
/// <summary>
7878
/// Create a FastBufferReader from a NativeArray.
79-
///
79+
///
8080
/// A new buffer will be created using the given allocator and the value will be copied in.
8181
/// FastBufferReader will then own the data.
8282
///
@@ -93,12 +93,12 @@ internal unsafe void CommitBitwiseReads(int amount)
9393
/// <param name="offset"></param>
9494
public unsafe FastBufferReader(NativeArray<byte> buffer, Allocator allocator, int length = -1, int offset = 0)
9595
{
96-
Handle = CreateHandle((byte*)buffer.GetUnsafePtr(), Math.Max(1, length == -1 ? buffer.Length : length), offset, allocator);
96+
Handle = CreateHandle((byte*)buffer.GetUnsafePtr(), length == -1 ? buffer.Length : length, offset, allocator);
9797
}
9898

9999
/// <summary>
100100
/// Create a FastBufferReader from an ArraySegment.
101-
///
101+
///
102102
/// A new buffer will be created using the given allocator and the value will be copied in.
103103
/// FastBufferReader will then own the data.
104104
///
@@ -117,13 +117,13 @@ public unsafe FastBufferReader(ArraySegment<byte> buffer, Allocator allocator, i
117117
}
118118
fixed (byte* data = buffer.Array)
119119
{
120-
Handle = CreateHandle(data, Math.Max(1, length == -1 ? buffer.Count : length), offset, allocator);
120+
Handle = CreateHandle(data, length == -1 ? buffer.Count : length, offset, allocator);
121121
}
122122
}
123123

124124
/// <summary>
125125
/// Create a FastBufferReader from an existing byte array.
126-
///
126+
///
127127
/// A new buffer will be created using the given allocator and the value will be copied in.
128128
/// FastBufferReader will then own the data.
129129
///
@@ -142,13 +142,13 @@ public unsafe FastBufferReader(byte[] buffer, Allocator allocator, int length =
142142
}
143143
fixed (byte* data = buffer)
144144
{
145-
Handle = CreateHandle(data, Math.Max(1, length == -1 ? buffer.Length : length), offset, allocator);
145+
Handle = CreateHandle(data, length == -1 ? buffer.Length : length, offset, allocator);
146146
}
147147
}
148148

149149
/// <summary>
150150
/// Create a FastBufferReader from an existing byte buffer.
151-
///
151+
///
152152
/// A new buffer will be created using the given allocator and the value will be copied in.
153153
/// FastBufferReader will then own the data.
154154
///
@@ -165,12 +165,12 @@ public unsafe FastBufferReader(byte[] buffer, Allocator allocator, int length =
165165
/// <param name="offset">The offset of the buffer to start copying from</param>
166166
public unsafe FastBufferReader(byte* buffer, Allocator allocator, int length, int offset = 0)
167167
{
168-
Handle = CreateHandle(buffer, Math.Max(1, length), offset, allocator);
168+
Handle = CreateHandle(buffer, length, offset, allocator);
169169
}
170170

171171
/// <summary>
172172
/// Create a FastBufferReader from a FastBufferWriter.
173-
///
173+
///
174174
/// A new buffer will be created using the given allocator and the value will be copied in.
175175
/// FastBufferReader will then own the data.
176176
///
@@ -187,7 +187,7 @@ public unsafe FastBufferReader(byte* buffer, Allocator allocator, int length, in
187187
/// <param name="offset">The offset of the buffer to start copying from</param>
188188
public unsafe FastBufferReader(FastBufferWriter writer, Allocator allocator, int length = -1, int offset = 0)
189189
{
190-
Handle = CreateHandle(writer.GetUnsafePtr(), Math.Max(1, length == -1 ? writer.Length : length), offset, allocator);
190+
Handle = CreateHandle(writer.GetUnsafePtr(), length == -1 ? writer.Length : length, offset, allocator);
191191
}
192192

193193
/// <summary>
@@ -250,7 +250,7 @@ public unsafe BitReader EnterBitwiseContext()
250250
/// When you know you will be reading multiple fields back-to-back and you know the total size,
251251
/// you can call TryBeginRead() once on the total size, and then follow it with calls to
252252
/// ReadValue() instead of ReadValueSafe() for faster serialization.
253-
///
253+
///
254254
/// Unsafe read operations will throw OverflowException in editor and development builds if you
255255
/// go past the point you've marked using TryBeginRead(). In release builds, OverflowException will not be thrown
256256
/// for performance reasons, since the point of using TryBeginRead is to avoid bounds checking in the following
@@ -284,7 +284,7 @@ public unsafe bool TryBeginRead(int bytes)
284284
/// When you know you will be reading multiple fields back-to-back and you know the total size,
285285
/// you can call TryBeginRead() once on the total size, and then follow it with calls to
286286
/// ReadValue() instead of ReadValueSafe() for faster serialization.
287-
///
287+
///
288288
/// Unsafe read operations will throw OverflowException in editor and development builds if you
289289
/// go past the point you've marked using TryBeginRead(). In release builds, OverflowException will not be thrown
290290
/// for performance reasons, since the point of using TryBeginRead is to avoid bounds checking in the following

com.unity.netcode.gameobjects/Tests/Editor/Serialization/FastBufferReaderTests.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,52 @@ public unsafe void GivenFastBufferReaderInitializedFromFastBufferWriterContainin
346346
}
347347
}
348348

349+
[Test]
350+
public void WhenCreatingAReaderFromAnEmptyArraySegment_LengthIsZero()
351+
{
352+
var bytes = new byte[] { };
353+
var input = new ArraySegment<byte>(bytes, 0, 0);
354+
using var reader = new FastBufferReader(input, Allocator.Temp);
355+
Assert.AreEqual(0, reader.Length);
356+
}
357+
358+
[Test]
359+
public void WhenCreatingAReaderFromAnEmptyArray_LengthIsZero()
360+
{
361+
var input = new byte[] { };
362+
using var reader = new FastBufferReader(input, Allocator.Temp);
363+
Assert.AreEqual(0, reader.Length);
364+
}
365+
366+
[Test]
367+
public void WhenCreatingAReaderFromAnEmptyNativeArray_LengthIsZero()
368+
{
369+
var input = new NativeArray<byte>(0, Allocator.Temp);
370+
using var reader = new FastBufferReader(input, Allocator.Temp);
371+
Assert.AreEqual(0, reader.Length);
372+
}
373+
374+
[Test]
375+
public void WhenCreatingAReaderFromAnEmptyFastBufferWriter_LengthIsZero()
376+
{
377+
var input = new FastBufferWriter(0, Allocator.Temp);
378+
using var reader = new FastBufferReader(input, Allocator.Temp);
379+
Assert.AreEqual(0, reader.Length);
380+
}
381+
382+
[Test]
383+
public void WhenCreatingAReaderFromAnEmptyBuffer_LengthIsZero()
384+
{
385+
var input = new byte[] { };
386+
unsafe
387+
{
388+
fixed (byte* ptr = input)
389+
{
390+
using var reader = new FastBufferReader(ptr, Allocator.Temp, 0);
391+
Assert.AreEqual(0, reader.Length);
392+
}
393+
}
394+
}
349395

350396
[Test]
351397
public void WhenCallingReadByteWithoutCallingTryBeingReadFirst_OverflowExceptionIsThrown()

0 commit comments

Comments
 (0)