diff options
author | Marek Safar <marek.safar@gmail.com> | 2013-03-01 17:11:23 +0400 |
---|---|---|
committer | Marek Safar <marek.safar@gmail.com> | 2013-03-01 17:11:23 +0400 |
commit | 59af20df27bb8c7428075429ee0a3bea0b89c0ce (patch) | |
tree | 7b77005b89b35be4caa66d36d941a9d63abe0c92 | |
parent | d47a9f26557f4e174fb8104315c23a11784d68fa (diff) | |
parent | 643d8b8b3a7eefe1f1e3a346faa0471e8d9fdf4b (diff) |
Merge pull request #584 from LogosBible/bug10789-resubmit
System.Text.Decoder throws ArgumentOutOfRangeException when it should no...
-rw-r--r-- | mcs/class/corlib/System.Text/Decoder.cs | 9 | ||||
-rw-r--r-- | mcs/class/corlib/System.Text/UTF8Encoding.cs | 2 | ||||
-rw-r--r-- | mcs/class/corlib/Test/System.Text/DecoderTest.cs | 17 | ||||
-rw-r--r-- | mcs/class/corlib/Test/System.Text/UTF8EncodingTest.cs | 22 |
4 files changed, 46 insertions, 4 deletions
diff --git a/mcs/class/corlib/System.Text/Decoder.cs b/mcs/class/corlib/System.Text/Decoder.cs index fe715c33796..cf2e6f23efc 100644 --- a/mcs/class/corlib/System.Text/Decoder.cs +++ b/mcs/class/corlib/System.Text/Decoder.cs @@ -149,7 +149,10 @@ public abstract class Decoder out int bytesUsed, out int charsUsed, out bool completed) { CheckArguments (bytes, byteIndex, byteCount); - CheckArguments (chars, charIndex); + if (chars == null) + throw new ArgumentNullException ("chars"); + if (charIndex < 0) + throw new ArgumentOutOfRangeException ("charIndex"); if (charCount < 0 || chars.Length < charIndex + charCount) throw new ArgumentOutOfRangeException ("charCount"); @@ -169,7 +172,7 @@ public abstract class Decoder { if (chars == null) throw new ArgumentNullException ("chars"); - if (charIndex < 0 || chars.Length <= charIndex) + if (charIndex < 0 || chars.Length < charIndex) throw new ArgumentOutOfRangeException ("charIndex"); } @@ -177,7 +180,7 @@ public abstract class Decoder { if (bytes == null) throw new ArgumentNullException ("bytes"); - if (byteIndex < 0 || bytes.Length <= byteIndex) + if (byteIndex < 0) throw new ArgumentOutOfRangeException ("byteIndex"); if (byteCount < 0 || bytes.Length < byteIndex + byteCount) throw new ArgumentOutOfRangeException ("byteCount"); diff --git a/mcs/class/corlib/System.Text/UTF8Encoding.cs b/mcs/class/corlib/System.Text/UTF8Encoding.cs index 3a1c2bd680f..bdd80e8a626 100644 --- a/mcs/class/corlib/System.Text/UTF8Encoding.cs +++ b/mcs/class/corlib/System.Text/UTF8Encoding.cs @@ -663,7 +663,7 @@ fail_no_space: throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array")); } - if (charIndex == chars.Length) + if (charIndex == chars.Length && byteCount == 0) return 0; fixed (char* cptr = chars) { diff --git a/mcs/class/corlib/Test/System.Text/DecoderTest.cs b/mcs/class/corlib/Test/System.Text/DecoderTest.cs index 3da0846360b..6cf0306d823 100644 --- a/mcs/class/corlib/Test/System.Text/DecoderTest.cs +++ b/mcs/class/corlib/Test/System.Text/DecoderTest.cs @@ -98,5 +98,22 @@ namespace MonoTests.System.Text } } #endif + + [Test] + public void Bug10789 () + { + byte[] bytes = new byte[100]; + char[] chars = new char[100]; + + Decoder conv = Encoding.UTF8.GetDecoder (); + int charsUsed, bytesUsed; + bool completed; + + conv.Convert (bytes, 0, 0, chars, 100, 0, false, out bytesUsed, out charsUsed, out completed); + + Assert.IsTrue (completed, "#1"); + Assert.AreEqual (0, charsUsed, "#2"); + Assert.AreEqual (0, bytesUsed, "#3"); + } } } diff --git a/mcs/class/corlib/Test/System.Text/UTF8EncodingTest.cs b/mcs/class/corlib/Test/System.Text/UTF8EncodingTest.cs index 022e8d7db7c..1f53af7e3eb 100644 --- a/mcs/class/corlib/Test/System.Text/UTF8EncodingTest.cs +++ b/mcs/class/corlib/Test/System.Text/UTF8EncodingTest.cs @@ -1174,5 +1174,27 @@ namespace MonoTests.System.Text Encoding.UTF8.GetDecoder ().GetChars (bytes, 0, 4096, chars, 9, false); } + + [Test] + public void Bug10789() + { + byte[] bytes = new byte[4096]; + char[] chars = new char[10]; + + try { + Encoding.UTF8.GetDecoder ().GetChars (bytes, 0, 1, chars, 10, false); + Assert.Fail ("ArgumentException is expected #1"); + } catch (ArgumentException) { + } + + try { + Encoding.UTF8.GetDecoder ().GetChars (bytes, 0, 1, chars, 11, false); + Assert.Fail ("ArgumentOutOfRangeException is expected #2"); + } catch (ArgumentOutOfRangeException) { + } + + int charactersWritten = Encoding.UTF8.GetDecoder ().GetChars (bytes, 0, 0, chars, 10, false); + Assert.AreEqual (0, charactersWritten, "#3"); + } } } |