summaryrefslogtreecommitdiff
path: root/src/mscorlib/shared
diff options
context:
space:
mode:
authorMaryam Ariyan <maryam.ariyan@microsoft.com>2018-03-26 21:56:35 -0700
committerJan Kotas <jkotas@microsoft.com>2018-03-26 21:56:35 -0700
commit24b9b0dd4de34628a815ef5f08a6e23a5416fa27 (patch)
treeaca56a7b7ddbf7c7ff51003fee22970dbcc9f1e3 /src/mscorlib/shared
parent37bd5d89ce760b412026296bd5a77495a1f80734 (diff)
downloadcoreclr-24b9b0dd4de34628a815ef5f08a6e23a5416fa27.tar.gz
coreclr-24b9b0dd4de34628a815ef5f08a6e23a5416fa27.tar.bz2
coreclr-24b9b0dd4de34628a815ef5f08a6e23a5416fa27.zip
New chunk should be equal/larger than the one it replaces (#17219)
* The new chunk should be equal or larger than the one it is replacing. Fixes #17205 * Adding .exe.stackdump to .gitignore
Diffstat (limited to 'src/mscorlib/shared')
-rw-r--r--src/mscorlib/shared/System/Text/StringBuilder.Debug.cs2
-rw-r--r--src/mscorlib/shared/System/Text/StringBuilder.cs24
2 files changed, 16 insertions, 10 deletions
diff --git a/src/mscorlib/shared/System/Text/StringBuilder.Debug.cs b/src/mscorlib/shared/System/Text/StringBuilder.Debug.cs
index c2c8c9f7fa..a62c4777ad 100644
--- a/src/mscorlib/shared/System/Text/StringBuilder.Debug.cs
+++ b/src/mscorlib/shared/System/Text/StringBuilder.Debug.cs
@@ -2,9 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using System.Collections.Generic;
using System.Diagnostics;
-using System.Runtime.Serialization;
namespace System.Text
{
diff --git a/src/mscorlib/shared/System/Text/StringBuilder.cs b/src/mscorlib/shared/System/Text/StringBuilder.cs
index 1a3208490f..1b5ea40dad 100644
--- a/src/mscorlib/shared/System/Text/StringBuilder.cs
+++ b/src/mscorlib/shared/System/Text/StringBuilder.cs
@@ -480,24 +480,32 @@ namespace System.Text
StringBuilder chunk = FindChunkForIndex(value);
if (chunk != this)
{
- // We crossed a chunk boundary when reducing the Length. We must replace this middle-chunk with a new larger chunk,
- // to ensure the original capacity is preserved.
-
// Avoid possible infinite capacity growth. See https://github.com/dotnet/coreclr/pull/16926
int capacityToPreserve = Math.Min(Capacity, Math.Max(Length * 6 / 5, m_ChunkChars.Length));
int newLen = capacityToPreserve - chunk.m_ChunkOffset;
- char[] newArray = new char[newLen];
-
- Debug.Assert(newLen > chunk.m_ChunkChars.Length, "The new chunk should be larger than the one it is replacing.");
- Array.Copy(chunk.m_ChunkChars, 0, newArray, 0, chunk.m_ChunkLength);
+ if (newLen > chunk.m_ChunkChars.Length)
+ {
+ // We crossed a chunk boundary when reducing the Length. We must replace this middle-chunk with a new larger chunk,
+ // to ensure the capacity we want is preserved.
+ char[] newArray = new char[newLen];
+ Array.Copy(chunk.m_ChunkChars, 0, newArray, 0, chunk.m_ChunkLength);
+ m_ChunkChars = newArray;
+ }
+ else
+ {
+ // Special case where the capacity we want to keep corresponds exactly to the size of the content.
+ // Just take ownership of the array.
+ Debug.Assert(newLen == chunk.m_ChunkChars.Length, "The new chunk should be larger or equal to the one it is replacing.");
+ m_ChunkChars = chunk.m_ChunkChars;
+ }
- m_ChunkChars = newArray;
m_ChunkPrevious = chunk.m_ChunkPrevious;
m_ChunkOffset = chunk.m_ChunkOffset;
}
m_ChunkLength = value - chunk.m_ChunkOffset;
AssertInvariants();
}
+ Debug.Assert(Length == value, "Something went wrong setting Length.");
}
}