Use EncodingForwarder for GetCharCount(byte*, int)
authorJames Ko <jamesqko@gmail.com>
Mon, 4 Jul 2016 17:08:37 +0000 (13:08 -0400)
committerJames Ko <jamesqko@gmail.com>
Mon, 4 Jul 2016 19:23:51 +0000 (15:23 -0400)
Commit migrated from https://github.com/dotnet/coreclr/commit/f83ecbead207000825555393287dafbca8259137

src/coreclr/src/mscorlib/src/System/Text/ASCIIEncoding.cs
src/coreclr/src/mscorlib/src/System/Text/EncodingForwarder.cs
src/coreclr/src/mscorlib/src/System/Text/EncodingNLS.cs
src/coreclr/src/mscorlib/src/System/Text/UTF32Encoding.cs
src/coreclr/src/mscorlib/src/System/Text/UTF7Encoding.cs
src/coreclr/src/mscorlib/src/System/Text/UTF8Encoding.cs
src/coreclr/src/mscorlib/src/System/Text/UnicodeEncoding.cs

index f94b70f..144dc9c 100644 (file)
@@ -155,17 +155,7 @@ namespace System.Text
         [System.Runtime.InteropServices.ComVisible(false)]
         public override unsafe int GetCharCount(byte* bytes, int count)
         {
-            // Validate Parameters
-            if (bytes == null)
-                throw new ArgumentNullException("bytes",
-                    Environment.GetResourceString("ArgumentNull_Array"));
-
-            if (count < 0)
-                throw new ArgumentOutOfRangeException("count",
-                    Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
-            Contract.EndContractBlock();
-
-            return GetCharCount(bytes, count, null);
+            return EncodingForwarder.GetCharCount(this, bytes, count);
         }
 
         // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
index 3d80700..5cd897c 100644 (file)
@@ -22,6 +22,14 @@ namespace System.Text
         // These set of methods exist so instead of duplicating code, we can
         // simply have those overriden methods call here to do the actual work.
 
+        // NOTE: This class should ONLY be called from Encodings that override
+        // the internal methods which accept an Encoder/DecoderNLS. The reason
+        // for this is that by default, those methods just call the same overload
+        // except without the encoder/decoder parameter. If an overriden method
+        // without that parameter calls this class, which calls the overload with
+        // the parameter, it will call the same method again, which will eventually
+        // lead to a StackOverflowException.
+
         public unsafe static int GetByteCount(Encoding encoding, char[] chars, int index, int count)
         {
             // Validate parameters
@@ -205,5 +213,21 @@ namespace System.Text
             fixed (byte* pBytes = bytes)
                 return encoding.GetCharCount(pBytes + index, count, decoder: null);
         }
+
+        public unsafe static int GetCharCount(Encoding encoding, byte* bytes, int count)
+        {
+            Contract.Assert(encoding != null);
+            if (bytes == null)
+            {
+                throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
+            }
+            if (count < 0)
+            {
+                throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
+            }
+            Contract.EndContractBlock();
+
+            return encoding.GetCharCount(bytes, count, decoder: null);
+        }
     }
 }
index 4d4c8cd..f684407 100644 (file)
@@ -125,17 +125,7 @@ namespace System.Text
         [System.Security.SecurityCritical]  // auto-generated
         public override unsafe int GetCharCount(byte* bytes, int count)
         {
-            // Validate Parameters
-            if (bytes == null)
-                throw new ArgumentNullException("bytes", 
-                    Environment.GetResourceString("ArgumentNull_Array"));
-
-            if (count < 0)
-                throw new ArgumentOutOfRangeException("count", 
-                    Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
-            Contract.EndContractBlock();
-            
-            return GetCharCount(bytes, count, null);
+            return EncodingForwarder.GetCharCount(this, bytes, count);
         }        
 
         // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
index 69172fa..3259d94 100644 (file)
@@ -188,17 +188,7 @@ namespace System.Text
         [CLSCompliant(false)]
         public override unsafe int GetCharCount(byte* bytes, int count)
         {
-            // Validate Parameters
-            if (bytes == null)
-                throw new ArgumentNullException("bytes",
-                    Environment.GetResourceString("ArgumentNull_Array"));
-
-            if (count < 0)
-                throw new ArgumentOutOfRangeException("count",
-                    Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
-            Contract.EndContractBlock();
-
-            return GetCharCount(bytes, count, null);
+            return EncodingForwarder.GetCharCount(this, bytes, count);
         }
 
         // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
index 42b481a..bac6556 100644 (file)
@@ -259,17 +259,7 @@ namespace System.Text
         [System.Runtime.InteropServices.ComVisible(false)]
         public override unsafe int GetCharCount(byte* bytes, int count)
         {
-            // Validate Parameters
-            if (bytes == null)
-                throw new ArgumentNullException("bytes",
-                    Environment.GetResourceString("ArgumentNull_Array"));
-
-            if (count < 0)
-                throw new ArgumentOutOfRangeException("count",
-                    Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
-            Contract.EndContractBlock();
-
-            return GetCharCount(bytes, count, null);
+            return EncodingForwarder.GetCharCount(this, bytes, count);
         }
 
         // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
index 96b83ff..3b8114f 100644 (file)
@@ -217,17 +217,7 @@ namespace System.Text
         [System.Runtime.InteropServices.ComVisible(false)]
         public override unsafe int GetCharCount(byte* bytes, int count)
         {
-            // Validate Parameters
-            if (bytes == null)
-                throw new ArgumentNullException("bytes",
-                    Environment.GetResourceString("ArgumentNull_Array"));
-
-            if (count < 0)
-                throw new ArgumentOutOfRangeException("count",
-                    Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
-            Contract.EndContractBlock();
-
-            return GetCharCount(bytes, count, null);
+            return EncodingForwarder.GetCharCount(this, bytes, count);
         }
 
         // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
index 4c5dff7..b52858a 100644 (file)
@@ -189,17 +189,7 @@ namespace System.Text
         [System.Runtime.InteropServices.ComVisible(false)]
         public override unsafe int GetCharCount(byte* bytes, int count)
         {
-            // Validate Parameters
-            if (bytes == null)
-                throw new ArgumentNullException("bytes",
-                    Environment.GetResourceString("ArgumentNull_Array"));
-
-            if (count < 0)
-                throw new ArgumentOutOfRangeException("count",
-                    Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
-            Contract.EndContractBlock();
-
-            return GetCharCount(bytes, count, null);
+            return EncodingForwarder.GetCharCount(this, bytes, count);
         }
 
         // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)