Skip to content

Commit e34dba5

Browse files
fix: FastBufferWriter growth from a size of 0 (#4223)
* 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 7951347 commit e34dba5

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
@@ -22,6 +22,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
2222

2323
### Fixed
2424

25+
- 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. (#4223)
2526
- 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. (#4221)
2627
- Issue where a client synchronized while a `NetworkList<T>` still had unsent changes applied those changes twice, resulting in duplicate entries and index-based events landing on the wrong elements on that client. (#4215)
2728
- Issue where showing a `NetworkObject` with `NetworkShow` while its `NetworkVariable` updates were held back by `NetworkVariableUpdateTraits` stopped those pending changes from being sent until the variable was modified again. (#4215)

‎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)