Skip to content

Write null arrays as empty in NetSerializer - #627

Merged
RevenantX merged 1 commit into
RevenantX:masterfrom
vtxe02023274:fix/serializer-null-arrays
Nov 28, 2026
Merged

RevenantX merged 1 commit into
RevenantX:masterfrom
vtxe02023274:fix/serializer-null-arrays

Conversation

@vtxe02023274

Copy link
Copy Markdown
Contributor

A null array property of a nested type throws in NetPacketProcessor.Write:

class Packet { public SomeStruct[] Items { get; set; } }   // SomeStruct registered with RegisterNestedType

processor.Write(writer, new Packet());   // NullReferenceException in WriteArrayHelper

WriteArrayHelper reads arr.Length without a null check. It is used for every array that does not go through NetDataWriter.PutArray: INetSerializable structs and classes, types registered with write/read delegates, and char, Guid, IPEndPoint. A null int[] or string[] property already works because PutArray writes null as an empty array, and null lists are written as empty since #626.

WriteArrayHelper now writes a zero count for a null array, so it is read back as an empty array like the other cases.

Test: NullArraysAreWrittenAsEmpty sets an int[], two delegate-registered struct arrays, an INetSerializable struct array and an INetSerializable class array to null and round trips the packet. It fails before the change (NullReferenceException) and passes after. dotnet test passes on net8.0 and net9.0 (84 tests).

WriteArrayHelper read arr.Length without a null check, so a null array
property of a nested or INetSerializable type (struct, class, a type
registered with RegisterNestedType write/read delegates, char, Guid,
IPEndPoint) threw NullReferenceException in NetPacketProcessor.Write.
Primitive and string arrays already go through NetDataWriter.PutArray,
which writes a null array as an empty one, and null lists are written
as empty since RevenantX#626.

Write a zero count for a null array, so it is read back as an empty
array like the other cases.
@RevenantX
RevenantX merged commit 67ac035 into RevenantX:master Nov 28, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants