From 753c750e37ba161604a2058d082cc0736172c9a4 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 22 Sep 2026 20:06:16 +0200 Subject: [PATCH] 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 --- src/GameCult.Networking/CultNetSelectionEvaluator.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/GameCult.Networking/CultNetSelectionEvaluator.cs b/src/GameCult.Networking/CultNetSelectionEvaluator.cs index 0e01ec0e..b48fb38a 100644 --- a/src/GameCult.Networking/CultNetSelectionEvaluator.cs +++ b/src/GameCult.Networking/CultNetSelectionEvaluator.cs @@ -620,9 +620,15 @@ namespace GameCult.Networking } /// Appends one length-prefixed string: unambiguous regardless of what the string itself contains. + // 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); } /// Appends a count-prefixed list of length-prefixed strings, sorted by code point so the digest does not depend on wire order.