Skip to content

Commit cea4486

Browse files
authored
fix: Fixes "writing past the end of the buffer" error when calling ResetDirty() on managed network variables that are larger than 256 bytes when serialized. (#2670)
1 parent cbc2d72 commit cea4486

File tree

4 files changed

+248
-156
lines changed

4 files changed

+248
-156
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
66

77
Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).
88

9+
## [Unreleased]
10+
11+
### Fixed
12+
13+
- Fixed "writing past the end of the buffer" error when calling ResetDirty() on managed network variables that are larger than 256 bytes when serialized.
14+
915
## [1.5.2] - 2023-07-24
1016

1117
### Added

com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariable.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,11 @@ public override void ResetDirty()
146146
// Therefore, we set the m_PreviousValue field to a duplicate of the current
147147
// field, so that our next dirty check is made against the current "not dirty"
148148
// value.
149-
m_HasPreviousValue = true;
150-
NetworkVariableSerialization<T>.Serializer.Duplicate(m_InternalValue, ref m_PreviousValue);
149+
if (!m_HasPreviousValue || !NetworkVariableSerialization<T>.AreEqual(ref m_InternalValue, ref m_PreviousValue))
150+
{
151+
m_HasPreviousValue = true;
152+
NetworkVariableSerialization<T>.Serializer.Duplicate(m_InternalValue, ref m_PreviousValue);
153+
}
151154
}
152155

153156
/// <summary>

com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableSerialization.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ void INetworkVariableSerializer<T>.ReadWithAllocator(FastBufferReader reader, ou
514514

515515
public void Duplicate(in T value, ref T duplicatedValue)
516516
{
517-
using var writer = new FastBufferWriter(256, Allocator.Temp);
517+
using var writer = new FastBufferWriter(256, Allocator.Temp, int.MaxValue);
518518
var refValue = value;
519519
Write(writer, ref refValue);
520520

0 commit comments

Comments
 (0)