From fcc0738c3c8ff738ed85fb2623124610d47ead02 Mon Sep 17 00:00:00 2001 From: skyoxZ Date: Sun, 14 Apr 2024 19:26:58 +0800 Subject: [PATCH 1/2] Update StringBuilder.cs --- .../System.Private.CoreLib/src/System/Text/StringBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs index 6f7b0abfa9b91f..f2da0e100b94bc 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs @@ -846,7 +846,7 @@ private StringBuilder AppendCore(StringBuilder value, int startIndex, int count) if (length == 0) { ExpandByABlock(count); - length = Math.Min(m_ChunkChars.Length - m_ChunkLength, count); + length = count; } value.CopyTo(startIndex, new Span(m_ChunkChars, m_ChunkLength, length), length); From ddb52abf7c8569df82a8925e4366f0e0bc61ea15 Mon Sep 17 00:00:00 2001 From: skyoxZ Date: Fri, 19 Apr 2024 07:14:10 +0800 Subject: [PATCH 2/2] Add an assert to validate `ExpandByABlock` working as expected --- .../System.Private.CoreLib/src/System/Text/StringBuilder.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs index f2da0e100b94bc..0622a934645e79 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs @@ -846,6 +846,7 @@ private StringBuilder AppendCore(StringBuilder value, int startIndex, int count) if (length == 0) { ExpandByABlock(count); + Debug.Assert(m_ChunkLength == 0 && m_ChunkChars.Length >= count); length = count; } value.CopyTo(startIndex, new Span(m_ChunkChars, m_ChunkLength, length), length);