[corlib] Add overflow checks to string padding
authorMarek Safar <marek.safar@gmail.com>
Tue, 4 Feb 2014 11:40:33 +0000 (12:40 +0100)
committerMarek Safar <marek.safar@gmail.com>
Tue, 4 Feb 2014 11:44:44 +0000 (12:44 +0100)
mcs/class/corlib/System/String.cs
mcs/class/corlib/Test/System/StringTest.cs

index c93d690866cf761603c3850c6c60dec49dc6fbed..5ab14ff1333d332633d9e7c5fe0b8c404ecfa577 100644 (file)
@@ -1596,7 +1596,13 @@ namespace System
 
                        fixed (char* dest = tmp, src = this) {
                                char* padPos = dest;
-                               char* padTo = dest + (totalWidth - length);
+                               char* padTo;
+                               try {
+                                       padTo = checked (dest + (totalWidth - length));
+                               } catch (OverflowException) {
+                                       throw new OutOfMemoryException ();
+                               }
+
                                while (padPos != padTo)
                                        *padPos++ = paddingChar;
 
@@ -1627,10 +1633,14 @@ namespace System
                        fixed (char* dest = tmp, src = this) {
                                CharCopy (dest, src, length);
 
-                               char* padPos = dest + length;
-                               char* padTo = dest + totalWidth;
-                               while (padPos != padTo)
-                                       *padPos++ = paddingChar;
+                               try {
+                                       char* padPos = checked (dest + length);
+                                       char* padTo = checked (dest + totalWidth);
+                                       while (padPos != padTo)
+                                               *padPos++ = paddingChar;
+                               } catch (OverflowException) {
+                                       throw new OutOfMemoryException ();
+                               }
                        }
                        return tmp;
                }
index 489070c6ad6cabf463912e720d2c2c1fe871a51e..68119d1c5afa8f28d8b940e92ce4cf602cee8cbc 100644 (file)
@@ -2976,6 +2976,15 @@ public class StringTest
                }
        }
 
+       [Test]
+       public void PadLeft_Overflow ()
+       {
+               try {
+                       "x".PadLeft (int.MaxValue, '-');
+               } catch (OutOfMemoryException) {
+               }
+       }
+
        [Test] // PadRight (Int32)
        public void PadRight1 ()
        {
@@ -3017,6 +3026,15 @@ public class StringTest
                Assert.AreEqual ("000000000000", "".PadRight (12, '0'), "#2");
        }
 
+       [Test]
+       public void PadRight_Overflow ()
+       {
+               try {
+                       "x".PadRight (int.MaxValue, '-');
+               } catch (OutOfMemoryException) {
+               }
+       }
+
        [Test] // Remove (Int32, Int32)
        public void Remove2 ()
        {