Fix range check in string::Split
authorMarek Safar <marek.safar@gmail.com>
Fri, 27 Apr 2012 16:25:01 +0000 (17:25 +0100)
committerMarek Safar <marek.safar@gmail.com>
Fri, 27 Apr 2012 16:25:57 +0000 (17:25 +0100)
mcs/class/corlib/System/String.cs
mcs/class/corlib/Test/System/StringTest.cs

index 2e7256dd6d6648eac2e61db57f65f7aa7197202a..1ab450e1c47e01aec936ae25491384e45c2168d5 100644 (file)
@@ -337,7 +337,7 @@ namespace System
                                        char* src_ptr = src;
                                        int len = Length;
 
-                                       do {
+                                       while (len > 0) {
                                                if (char.IsWhiteSpace (*src_ptr++)) {
                                                        if (split_points == null) {
                                                                split_points = new int[8];
@@ -349,7 +349,8 @@ namespace System
                                                        if (total_points == count && !removeEmpty)
                                                                break;
                                                }
-                                       } while (len-- != 0);
+                                               --len;
+                                       }
                                }
                        } else {
                                fixed (char* src = this) {
@@ -357,7 +358,7 @@ namespace System
                                                char* src_ptr = src;
                                                char* sep_ptr_end = sep_src + sep.Length;
                                                int len = Length;
-                                               do {
+                                               while (len > 0) {
                                                        char* sep_ptr = sep_src;
                                                        do {
                                                                if (*sep_ptr++ == *src_ptr) {
@@ -376,7 +377,8 @@ namespace System
                                                        } while (sep_ptr != sep_ptr_end);
 
                                                        ++src_ptr;
-                                               } while (len-- != 0);
+                                                       --len;
+                                               }
                                        }
                                }
                        }
index f2c23ac6a6268568fadb44c0aaebefe8fd0cccda..402c6d88c1b567bdc09db4b1c3943b3e7a37da91 100644 (file)
@@ -4133,6 +4133,10 @@ public class StringTest
                Assert.AreEqual (2, res.Length, "#11-09-3");
 
                Assert.AreEqual (0, "    ".Split ((char[]) null, 2, StringSplitOptions.RemoveEmptyEntries).Length, "#12-00-0");
+               
+               res = "not found".Split (new char[2]);
+               Assert.AreEqual ("not found", res[0], "#12-04-27");
+               Assert.AreEqual (1, res.Length, "#12-04-27-A");
        }
        
        [Test]