[I18N] Assertions to prevent a buffer overrun.
authorMark Probst <mark.probst@gmail.com>
Wed, 23 Jan 2013 11:46:00 +0000 (12:46 +0100)
committerMark Probst <mark.probst@gmail.com>
Wed, 23 Jan 2013 11:46:41 +0000 (12:46 +0100)
The overrun bug (#9578) is still there, but at least now it's not triggered.

mcs/class/I18N/Common/ByteEncoding.cs
mcs/class/I18N/MidEast/CP1254.cs

index 257424e8d1e711a271c48b7ec53c346584547166..baebe23fc2eb0e83ca89fe811442c561e8400c50 100644 (file)
@@ -157,6 +157,18 @@ public abstract class ByteEncoding : MonoEncoding
                // (this is the ideal solution)
                if (charCount == 0 || bytes.Length == byteIndex)
                        return;
+               if (charIndex < 0 || charIndex > chars.Length) {
+                       throw new ArgumentOutOfRangeException
+                               ("charIndex", Strings.GetString("ArgRange_Array"));
+               }
+               if (byteIndex < 0 || byteIndex > bytes.Length) {
+                       throw new ArgumentOutOfRangeException
+                               ("byteIndex", Strings.GetString("ArgRange_Array"));
+               }
+               if (charCount < 0 || charIndex + charCount > chars.Length || byteIndex + charCount > bytes.Length) {
+                       throw new ArgumentOutOfRangeException
+                               ("charCount", Strings.GetString("ArgRange_Array"));
+               }
                fixed (char* cptr = chars) {
                        fixed (byte* bptr = bytes) {
                                ToBytes (cptr + charIndex, charCount,
index 9d3ad3aefd291d9b9c2e33eff6bbb435ee7f374c..f16a1801c7f6ce33e3af345ac2701a2534c6dfa4 100644 (file)
@@ -95,6 +95,10 @@ public class CP1254 : ByteEncoding
 #if NET_2_0
                EncoderFallbackBuffer buffer = null;
 #endif
+               if (byteCount < charCount) {
+                       throw new ArgumentOutOfRangeException
+                               ("byteCount", Strings.GetString("ArgRange_Array"));
+               }
                while(charCount > 0)
                {
                        ch = (int)(chars[charIndex++]);
@@ -233,12 +237,17 @@ public class CP1254 : ByteEncoding
                                {
                                        if(ch >= 0xFF01 && ch <= 0xFF5E)
                                                ch -= 0xFEE0;
-                                       else
+                                       else {
 #if NET_2_0
                                                HandleFallback (ref buffer, chars, ref charIndex, ref charCount, bytes, ref byteIndex, ref byteCount);
+                                               if (byteCount < charCount) {
+                                                       throw new ArgumentOutOfRangeException
+                                                               ("byteCount", Strings.GetString("ArgRange_Array"));
+                                               }
 #else
                                                ch = 0x3F;
 #endif
+                                       }
                                }
                                break;
                        }