From 2d8031df3c7e920fd1578054ebccaeb8a250e4e5 Mon Sep 17 00:00:00 2001 From: Raja R Harinath Date: Mon, 7 Sep 2009 17:14:37 +0000 Subject: [PATCH 1/1] regex: A few more null and bounds checks * 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 --- .../System.Text.RegularExpressions/ChangeLog | 6 +++ .../System.Text.RegularExpressions/Regex.cs | 24 +++++++++++ .../RegexTest.cs | 42 +++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/mcs/class/System/System.Text.RegularExpressions/ChangeLog b/mcs/class/System/System.Text.RegularExpressions/ChangeLog index 69aa14c6413..156a4207afc 100644 --- a/mcs/class/System/System.Text.RegularExpressions/ChangeLog +++ b/mcs/class/System/System.Text.RegularExpressions/ChangeLog @@ -1,3 +1,9 @@ +2009-09-07 Raja R Harinath + + * 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 Add some null checks and bounds checks. diff --git a/mcs/class/System/System.Text.RegularExpressions/Regex.cs b/mcs/class/System/System.Text.RegularExpressions/Regex.cs index 2e964341c75..4a425a9d90e 100644 --- a/mcs/class/System/System.Text.RegularExpressions/Regex.cs +++ b/mcs/class/System/System.Text.RegularExpressions/Regex.cs @@ -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); } diff --git a/mcs/class/System/Test/System.Text.RegularExpressions/RegexTest.cs b/mcs/class/System/Test/System.Text.RegularExpressions/RegexTest.cs index 4c4f656f9bf..6f55bbeff08 100644 --- a/mcs/class/System/Test/System.Text.RegularExpressions/RegexTest.cs +++ b/mcs/class/System/Test/System.Text.RegularExpressions/RegexTest.cs @@ -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" + -- 2.25.1