From 41fd0760dfc4e98326d0cc30a0f9d791ab0896ab Mon Sep 17 00:00:00 2001 From: Blank Date: Wed, 29 Jul 2026 18:07:05 +0800 Subject: [PATCH] =?UTF-8?q?fix(extensions):=20=E6=B6=88=E9=99=A4=20SpanExt?= =?UTF-8?q?ensions.WriteBytesWithoutLength=20=E7=9A=84=20Sonar=20S6640=20u?= =?UTF-8?q?nsafe=20=E8=BF=9D=E8=A7=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Span.WriteBytesWithoutLength 移除 unsafe 关键字与 fixed (byte*) 块,将 Buffer.MemoryCopy 替换为 value.AsSpan().CopyTo(buffer[offset..]),对齐姐妹方法 WriteBytesWithoutLengthLittleEndian 的安全写法。公共签名、参数校验、异常文案、offset 自增均保持不变;现有 WriteBytesWithoutLengthBigEndian_* 测试与 RoundTrip 测试覆盖了正常写入/null/负 offset/越界/空数组/往返,全部通过。 Linear: GFX-193 --- GameFrameX.Foundation.Extensions/SpanExtensions.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/GameFrameX.Foundation.Extensions/SpanExtensions.cs b/GameFrameX.Foundation.Extensions/SpanExtensions.cs index 86e372f..4948bec 100644 --- a/GameFrameX.Foundation.Extensions/SpanExtensions.cs +++ b/GameFrameX.Foundation.Extensions/SpanExtensions.cs @@ -135,7 +135,7 @@ public static void WriteBoolValue(this Span buffer, bool value, ref int of /// 读写操作的起始位置,写入后会自动增加相应字节数 / The starting position for read/write, automatically increments by the corresponding number of bytes after writing. /// 为 null 时抛出 / Thrown when is null. /// 为负数或超出缓冲区边界时抛出 / Thrown when is negative or exceeds buffer bounds. - public static unsafe void WriteBytesWithoutLength(this Span buffer, byte[] value, ref int offset) + public static void WriteBytesWithoutLength(this Span buffer, byte[] value, ref int offset) { ArgumentNullException.ThrowIfNull(value); ArgumentOutOfRangeException.ThrowIfNegative(offset, nameof(offset)); @@ -145,11 +145,8 @@ public static unsafe void WriteBytesWithoutLength(this Span buffer, byte[] throw new ArgumentOutOfRangeException(nameof(offset), LocalizationService.GetString(LocalizationKeys.Exceptions.OffsetOutsideBufferBounds, offset, value.Length, buffer.Length)); } - fixed (byte* ptr = buffer, valPtr = value) - { - Buffer.MemoryCopy(valPtr, ptr + offset, value.Length, value.Length); - offset += value.Length; - } + value.AsSpan().CopyTo(buffer[offset..]); + offset += value.Length; } ///