regex: A few more null and bounds checks
authorRaja R Harinath <harinath@hurrynot.org>
Mon, 7 Sep 2009 17:14:37 +0000 (17:14 -0000)
committerRaja R Harinath <harinath@hurrynot.org>
Mon, 7 Sep 2009 17:14:37 +0000 (17:14 -0000)
* Regex.cs (Escape, Unescape): Add null checks.
(validate_options): Add ECMAScript option checks.
(Replace, Split): Add a few bounds checks.

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

mcs/class/System/System.Text.RegularExpressions/ChangeLog
mcs/class/System/System.Text.RegularExpressions/Regex.cs
mcs/class/System/Test/System.Text.RegularExpressions/RegexTest.cs

index 69aa14c641353485876f4b9cdf9c890930e8b24e..156a4207afc254aeb7bb8709b86596c7ff22d7a1 100644 (file)
@@ -1,3 +1,9 @@
+2009-09-07  Raja R Harinath  <harinath@hurrynot.org>
+
+       * Regex.cs (Escape, Unescape): Add null checks.
+       (validate_options): Add ECMAScript option checks.
+       (Replace, Split): Add a few bounds checks.
+
 2009-09-07  Raja R Harinath  <harinath@hurrynot.org>
 
        Add some null checks and bounds checks.
index 2e964341c75a32debf876fd093d694284306f284..4a425a9d90e055026f7823606329f8f6697187f0 100644 (file)
@@ -97,11 +97,15 @@ namespace System.Text.RegularExpressions {
                
                public static string Escape (string str)
                {
+                       if (str == null)
+                               throw new ArgumentNullException ("str");
                        return Parser.Escape (str);
                }
 
                public static string Unescape (string str)
                {
+                       if (str == null)
+                               throw new ArgumentNullException ("str");
                        return Parser.Unescape (str);
                }
 
@@ -228,8 +232,19 @@ namespace System.Text.RegularExpressions {
                                RegexOptions.RightToLeft |
                                RegexOptions.ECMAScript |
                                RegexOptions.CultureInvariant;
+
+                       const RegexOptions ecmaopts =
+                               RegexOptions.IgnoreCase |
+                               RegexOptions.Multiline |
+#if !NET_2_1
+                               RegexOptions.Compiled |
+#endif
+                               RegexOptions.ECMAScript;
+
                        if ((options & ~allopts) != 0)
                                throw new ArgumentOutOfRangeException ("options");
+                       if ((options & RegexOptions.ECMAScript) != 0 && (options & ~ecmaopts) != 0)
+                               throw new ArgumentOutOfRangeException ("options");
                }
 
 #if !TARGET_JVM
@@ -445,6 +460,8 @@ namespace System.Text.RegularExpressions {
                                throw new ArgumentNullException ("evaluator");
                        if (count < -1)
                                throw new ArgumentOutOfRangeException ("count");
+                       if (startat < 0 || startat > input.Length)
+                               throw new ArgumentOutOfRangeException ("startat");
 
                        BaseMachine m = (BaseMachine)CreateMachine ();
 
@@ -477,6 +494,8 @@ namespace System.Text.RegularExpressions {
                                throw new ArgumentNullException ("replacement");
                        if (count < -1)
                                throw new ArgumentOutOfRangeException ("count");
+                       if (startat < 0 || startat > input.Length)
+                               throw new ArgumentOutOfRangeException ("startat");
 
                        return CreateMachine ().Replace (this, input, replacement, count, startat);
                }
@@ -497,6 +516,11 @@ namespace System.Text.RegularExpressions {
                {
                        if (input == null)
                                throw new ArgumentNullException ("input");
+                       if (count < 0)
+                               throw new ArgumentOutOfRangeException ("count");
+                       if (startat < 0 || startat > input.Length)
+                               throw new ArgumentOutOfRangeException ("startat");
+
                        return CreateMachine ().Split (this, input, count, startat);
                }
 
index 4c4f656f9bf818ab254a5adf4d3d204131c53caf..6f55bbeff08f55e3d45b77ae1dd2de856f6b9e26 100644 (file)
@@ -63,6 +63,12 @@ namespace MonoTests.System.Text.RegularExpressions
                {
                        new Regex ("foo", (RegexOptions) Int32.MaxValue);
                }
+
+               [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
+               public void InvalidOptions2 ()
+               {
+                       new Regex ("foo", RegexOptions.ECMAScript | RegexOptions.RightToLeft);
+               }
                
                [Test]
                public void Unescape ()
@@ -213,6 +219,18 @@ namespace MonoTests.System.Text.RegularExpressions
                        r.Replace ("string", m, 0, 0);
                }
 
+               [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
+               public void Replace_InvalidCount ()
+               {
+                       Regex.Replace ("foo", "foo|bar", "baz", -4);
+               }
+
+               [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
+               public void Replace_InvalidStart ()
+               {
+                       Regex.Replace ("foo", "foo|bar", "baz", 1, -4);
+               }
+
                [Test, ExpectedException (typeof (ArgumentNullException))]
                public void Split_InputNull1 ()
                {
@@ -225,6 +243,30 @@ namespace MonoTests.System.Text.RegularExpressions
                        Regex.Split (null, "^.*$", RegexOptions.RightToLeft);
                }
 
+               [Test, ExpectedException (typeof (ArgumentNullException))]
+               public void Split_InvalidCount ()
+               {
+                       Regex.Split (null, "^.*$", -4);
+               }
+
+               [Test, ExpectedException (typeof (ArgumentNullException))]
+               public void Split_InvalidCount ()
+               {
+                       Regex.Split (null, "^.*$", 1, -4);
+               }
+
+               [Test, ExpectedException (typeof (ArgumentNullException))]
+               public void Escape_Null ()
+               {
+                       Regex.Escape (null);
+               }
+
+               [Test, ExpectedException (typeof (ArgumentNullException))]
+               public void Unescape_Null ()
+               {
+                       Regex.Unescape (null);
+               }
+
                static string story =
                        "Two little dragons lived in the forest\n" +
                        "They spent their days collecting honey suckle,\n" +