Skip to content

Commit 699cb27

Browse files
fix: FastBufferWriter growth from a size of 0 (up-port) (#4224)
* fix: FastBufferWriter growth from a size of 0 A growable FastBufferWriter created with a size of 0 never returned from its first write that needed to grow: Grow doubled a capacity of 0, so its loop never reached the required size. Growth now starts from at least 1 byte. UUM-158589 * chore: CHANGELOG PR number
1 parent 37e00de commit 699cb27

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎com.unity.netcode.gameobjects/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
2424

2525
### Fixed
2626

27+
- Issue where a growable `FastBufferWriter` created with a size of 0 never returned from its first write or `TryBeginWrite` call that needed the buffer to grow. (#4224)
2728
- Issue where a `FastBufferReader` created with an offset and the default length of -1 read past the end of its source. The `ArraySegment<byte>` overload that takes an offset now applies it from the start of the segment. (#4222)
2829
- Issue where a client promoted to session owner could throw an exception when synchronizing a newly joining client. (#4214)
2930
- Issue where `NetworkManager.OnNetCodeDisconnect` was not invoked when a connection closed during a hybrid mode session, only when the session shut down. (#4209)

‎com.unity.netcode.gameobjects/Runtime/Serialization/FastBufferWriter.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public unsafe BitWriter EnterBitwiseContext()
193193

194194
internal unsafe void Grow(int additionalSizeRequired)
195195
{
196-
var desiredSize = Handle->Capacity * 2;
196+
var desiredSize = Math.Max(Handle->Capacity * 2, 1);
197197
while (desiredSize < Position + additionalSizeRequired)
198198
{
199199
desiredSize *= 2;

‎com.unity.netcode.gameobjects/Tests/Editor/Serialization/FastBufferWriterTests.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,17 @@ public void WhenBufferGrowthRequiredIsMoreThanDouble_BufferGrowsEnoughToContainR
15241524
}
15251525
}
15261526

1527+
[Test]
1528+
public void WhenGrowingWriterStartsWithZeroSize_BufferGrows()
1529+
{
1530+
using var growingWriter = new FastBufferWriter(0, Allocator.Temp, 64);
1531+
Assert.IsTrue(growingWriter.TryBeginWrite(4));
1532+
Assert.AreEqual(4, growingWriter.Capacity);
1533+
1534+
growingWriter.WriteByteSafe(1);
1535+
Assert.AreEqual(new byte[] { 1 }, growingWriter.ToArray());
1536+
}
1537+
15271538
[Test]
15281539
public void WhenTryingToWritePastMaxCapacity_GrowthDoesNotOccurAndTryBeginWriteReturnsFalse()
15291540
{

0 commit comments

Comments
 (0)