Fix the cursor body's length prefix to count UTF-8 bytes, not UTF-16 chars

AppendString (CultNetSelectionEvaluator.cs) wrote value.Length, .NET's UTF-16
code-unit count, but Cursor.Parse's ReadLengthPrefixedFields slices that many
bytes out of the UTF-8-encoded body. An astral character (a surrogate pair:
2 UTF-16 units, 4 UTF-8 bytes) made the two counts disagree, so a cursor
minted for a row keyed with one (Evaluator_CursorPositionUsesCodePointOrderMatchingTheRowTiebreak's
astral key) failed to parse back - caught by that existing test on Yggdrasil,
not a new one. The prefix is now Encoding.UTF8.GetByteCount(value); the
digest's canonical string never round-trips through the byte-sliced reader
(it is hashed whole), so this only had to be right for the cursor body, but
one counting rule for both is what keeps AppendString actually unambiguous.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-22 20:06:16 +02:00
commit 753c750e37

View file

@ -620,9 +620,15 @@ namespace GameCult.Networking
}
/// <summary>Appends one length-prefixed string: unambiguous regardless of what the string itself contains.</summary>
// The length prefix is a UTF-8 BYTE count, not string.Length's UTF-16 code-unit count - the
// cursor body's prefix is read back out of UTF-8 bytes (ReadLengthPrefixedFields), and an
// astral character (a surrogate pair: 2 UTF-16 code units, 4 UTF-8 bytes) would otherwise
// slice the wrong number of bytes on the way back. The digest's canonical string never
// round-trips through this byte-sliced reader - it is hashed whole - but keeping one counting
// rule for both uses is what makes AppendString unambiguous rather than merely usually right.
private static void AppendString(StringBuilder sb, string value)
{
sb.Append(value.Length).Append(':').Append(value);
sb.Append(Encoding.UTF8.GetByteCount(value)).Append(':').Append(value);
}
/// <summary>Appends a count-prefixed list of length-prefixed strings, sorted by code point so the digest does not depend on wire order.</summary>