From 1ebaf5390d06708e1e80ea090fd8a1069c565cd3 Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Wed, 11 Oct 2023 13:41:24 -0700 Subject: [PATCH] fix: Fixes span reader ReadString (#1539) --- Projects/Server/Buffers/SpanReader.cs | 11 +++++++++-- Projects/Server/Text/StringHelpers.cs | 1 + .../Engines/Quests/Core/Items/DynamicTeleporter.cs | 2 +- .../Emino's Undertaking/Items/EminosKatanaChest.cs | 2 +- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Projects/Server/Buffers/SpanReader.cs b/Projects/Server/Buffers/SpanReader.cs index d6ccffb25..31dc8e46f 100644 --- a/Projects/Server/Buffers/SpanReader.cs +++ b/Projects/Server/Buffers/SpanReader.cs @@ -168,6 +168,11 @@ public ref struct SpanReader [MethodImpl(MethodImplOptions.AggressiveInlining)] public string ReadString(Encoding encoding, bool safeString = false, int fixedLength = -1) { + if (fixedLength == 0) + { + return ""; + } + int byteLength = encoding.GetByteLengthForEncoding(); bool isFixedLength = fixedLength > -1; @@ -191,8 +196,10 @@ public ref struct SpanReader var span = _buffer.Slice(Position, size); var index = span.IndexOfTerminator(byteLength); - Position += isFixedLength || index < 0 ? size : index; - return TextEncoding.GetString(span[..index], encoding, safeString); + Position += isFixedLength || index < 0 ? size : index; + + // The string is either as long as the first terminator character, remaining buffer size, or fixed length. + return TextEncoding.GetString(span[..(index < 0 ? size : index)], encoding, safeString); } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/Projects/Server/Text/StringHelpers.cs b/Projects/Server/Text/StringHelpers.cs index 6879d61ef..c908b20f5 100644 --- a/Projects/Server/Text/StringHelpers.cs +++ b/Projects/Server/Text/StringHelpers.cs @@ -277,6 +277,7 @@ public static class StringHelpers _ => buffer.IndexOf((byte)0) }; + // TODO: If returns -1, do not multiply by the byte length [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int IndexOfTerminator(this ReadOnlySpan buffer, int sizeT) => sizeT switch diff --git a/Projects/UOContent/Engines/Quests/Core/Items/DynamicTeleporter.cs b/Projects/UOContent/Engines/Quests/Core/Items/DynamicTeleporter.cs index 3f3baf456..0c588b7e1 100644 --- a/Projects/UOContent/Engines/Quests/Core/Items/DynamicTeleporter.cs +++ b/Projects/UOContent/Engines/Quests/Core/Items/DynamicTeleporter.cs @@ -3,7 +3,7 @@ using Server.Mobiles; namespace Server.Engines.Quests; -[SerializationGenerator(0)] +[SerializationGenerator(0, false)] public abstract partial class DynamicTeleporter : Item { public DynamicTeleporter(int itemID = 0x1822, int hue = 0x482) : base(itemID) diff --git a/Projects/UOContent/Engines/Quests/Emino's Undertaking/Items/EminosKatanaChest.cs b/Projects/UOContent/Engines/Quests/Emino's Undertaking/Items/EminosKatanaChest.cs index e897307bc..71a7eb21f 100644 --- a/Projects/UOContent/Engines/Quests/Emino's Undertaking/Items/EminosKatanaChest.cs +++ b/Projects/UOContent/Engines/Quests/Emino's Undertaking/Items/EminosKatanaChest.cs @@ -5,7 +5,7 @@ using Server.Network; namespace Server.Engines.Quests.Ninja; -[SerializationGenerator(0, false)] +[SerializationGenerator(0)] public partial class EminosKatanaChest : WoodenChest { [Constructible]