Sfoglia il codice sorgente

In Test/System.Text:
2005-05-06 Ben Maurer <[email protected]>

* StringBuilderTest.cs (MaxCapacity_Overflow3): Test for #72244.

In System.Text:
2005-05-06 Ben Maurer <[email protected]>

* StringBuilder.cs (InternalEnsureCapacity): It is possible that
the size we attempt to grow to is more than the max capacity, but
that a smaller size will do. In this case, don't throw an
exception. Fixes #72244


svn path=/trunk/mcs/; revision=44166

Ben Maurer 20 anni fa
parent
commit
dcd2d2d7d1

+ 7 - 0
mcs/class/corlib/System.Text/ChangeLog

@@ -1,3 +1,10 @@
+2005-05-06  Ben Maurer  <[email protected]>
+
+	* StringBuilder.cs (InternalEnsureCapacity): It is possible that
+	the size we attempt to grow to is more than the max capacity, but
+	that a smaller size will do. In this case, don't throw an
+	exception. Fixes #72244
+
 2005-04-16  Atsushi Enomoto  <[email protected]>
 
 	* NormalizationForm.cs : new file.

+ 4 - 2
mcs/class/corlib/System.Text/StringBuilder.cs

@@ -644,8 +644,7 @@ namespace System.Text {
 				int capacity = _str.Length;
 
 				// Try double buffer, if that doesn't work, set the length as capacity
-				if (size > capacity) 
-				{
+				if (size > capacity) {
 					
 					// The first time a string is appended, we just set _cached_str
 					// and _str to it. This allows us to do some optimizations.
@@ -660,6 +659,9 @@ namespace System.Text {
 					if (capacity >= Int32.MaxValue || capacity < 0)
 						capacity = Int32.MaxValue;
 
+					if (capacity > _maxCapacity && size <= _maxCapacity)
+						capacity = _maxCapacity;
+					
 					if (capacity > _maxCapacity)
 						throw new ArgumentOutOfRangeException ("size", "capacity was less than the current size.");
 				}

+ 4 - 0
mcs/class/corlib/Test/System.Text/ChangeLog

@@ -1,3 +1,7 @@
+2005-05-06  Ben Maurer  <[email protected]>
+
+	* StringBuilderTest.cs (MaxCapacity_Overflow3): Test for #72244.
+
 2005-01-21  Ben Maurer  <[email protected]>
 
 	* StringBuilderTest.cs (CapacityFromString): This relies on impl

+ 12 - 1
mcs/class/corlib/Test/System.Text/StringBuilderTest.cs

@@ -441,8 +441,19 @@ namespace MonoTests.System.Text {
 
 		AssertEquals (2, sb.Capacity);
 		AssertEquals (3, sb.MaxCapacity);
+	}
+	
+	[Test]
+	public void MaxCapacity_Overflow3 ()
+	{
+		//
+		// When the capacity (4) gets doubled, it is greater than the
+		// max capacity. This makes sure that before throwing an exception
+		// we first attempt to go for a smaller size.
+		//
+		new StringBuilder(4, 7).Append ("foo").Append ("bar");
+		new StringBuilder(4, 6).Append ("foo").Append ("bar");
 	}
-
 	[Test]
 	public void CapacityFromString ()
 	{