Skip to content

Commit 24b9b0d

Browse files
maryamariyanjkotas
authored andcommitted
New chunk should be equal/larger than the one it replaces (dotnet#17219)
* The new chunk should be equal or larger than the one it is replacing. Fixes #17205 * Adding .exe.stackdump to .gitignore
1 parent 37bd5d8 commit 24b9b0d

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ $RECYCLE.BIN/
236236

237237
# Common binary extensions on Windows
238238
*.exe
239+
*.exe.stackdump
239240
*.dll
240241
*.lib
241242

src/mscorlib/shared/System/Text/StringBuilder.Debug.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Collections.Generic;
65
using System.Diagnostics;
7-
using System.Runtime.Serialization;
86

97
namespace System.Text
108
{

src/mscorlib/shared/System/Text/StringBuilder.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -480,24 +480,32 @@ public int Length
480480
StringBuilder chunk = FindChunkForIndex(value);
481481
if (chunk != this)
482482
{
483-
// We crossed a chunk boundary when reducing the Length. We must replace this middle-chunk with a new larger chunk,
484-
// to ensure the original capacity is preserved.
485-
486483
// Avoid possible infinite capacity growth. See https://github.com/dotnet/coreclr/pull/16926
487484
int capacityToPreserve = Math.Min(Capacity, Math.Max(Length * 6 / 5, m_ChunkChars.Length));
488485
int newLen = capacityToPreserve - chunk.m_ChunkOffset;
489-
char[] newArray = new char[newLen];
490-
491-
Debug.Assert(newLen > chunk.m_ChunkChars.Length, "The new chunk should be larger than the one it is replacing.");
492-
Array.Copy(chunk.m_ChunkChars, 0, newArray, 0, chunk.m_ChunkLength);
486+
if (newLen > chunk.m_ChunkChars.Length)
487+
{
488+
// We crossed a chunk boundary when reducing the Length. We must replace this middle-chunk with a new larger chunk,
489+
// to ensure the capacity we want is preserved.
490+
char[] newArray = new char[newLen];
491+
Array.Copy(chunk.m_ChunkChars, 0, newArray, 0, chunk.m_ChunkLength);
492+
m_ChunkChars = newArray;
493+
}
494+
else
495+
{
496+
// Special case where the capacity we want to keep corresponds exactly to the size of the content.
497+
// Just take ownership of the array.
498+
Debug.Assert(newLen == chunk.m_ChunkChars.Length, "The new chunk should be larger or equal to the one it is replacing.");
499+
m_ChunkChars = chunk.m_ChunkChars;
500+
}
493501

494-
m_ChunkChars = newArray;
495502
m_ChunkPrevious = chunk.m_ChunkPrevious;
496503
m_ChunkOffset = chunk.m_ChunkOffset;
497504
}
498505
m_ChunkLength = value - chunk.m_ChunkOffset;
499506
AssertInvariants();
500507
}
508+
Debug.Assert(Length == value, "Something went wrong setting Length.");
501509
}
502510
}
503511

0 commit comments

Comments
 (0)