Browse Source

2006-02-03 Atsushi Enomoto <[email protected]>

	* UTF8Encoding.cs : In zero-charbuffer case, byte buffer count should
	  be checked. Also it should not ignore leftOver characters even if
	  byte buffer length is 0.

	* UTF8EncodingTest.cs : added test for insufficient bytes for
	  flush=true and leftOver!='\0' case.


svn path=/trunk/mcs/; revision=56533
Atsushi Eno 20 years ago
parent
commit
e85a1fe77c

+ 6 - 0
mcs/class/corlib/System.Text/ChangeLog

@@ -1,3 +1,9 @@
+2006-02-03  Atsushi Enomoto  <[email protected]>
+
+	* UTF8Encoding.cs : In zero-charbuffer case, byte buffer count should
+	  be checked. Also it should not ignore leftOver characters even if
+	  byte buffer length is 0.
+
 2006-02-03  Atsushi Enomoto  <[email protected]>
 
 	* UTF8Encoding.cs : Fast path optimization for InternalGetByteCount()

+ 22 - 2
mcs/class/corlib/System.Text/UTF8Encoding.cs

@@ -225,19 +225,32 @@ public class UTF8Encoding : Encoding
 		}
 
 		if (charIndex == chars.Length) {
-			if (flush && leftOver != 0) {
+			if (flush && leftOver != '\0') {
+#if NET_2_0
+				// FIXME: use EncoderFallback.
+				//
+				// By default it is empty, so I do nothing for now.
+				leftOver = '\0';
+#else
 				// Flush the left-over surrogate pair start.
+				if (byteIndex + 3 >= bytes.Length)
+					throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
 				bytes [byteIndex++] = 0xEF;
 				bytes [byteIndex++] = 0xBB;
 				bytes [byteIndex++] = 0xBF;
 				leftOver = '\0';
 				return 3;
+#endif
 			}
 			return 0;
 		}
 
 		unsafe {
 			fixed (char* cptr = chars) {
+				if (bytes.Length == byteIndex)
+					return InternalGetBytes (
+						cptr + charIndex, charCount, 
+						null, 0, ref leftOver, flush);
 				fixed (byte *bptr = bytes) {
 					return InternalGetBytes (
 						cptr + charIndex, charCount,
@@ -362,6 +375,9 @@ public class UTF8Encoding : Encoding
 			}
 			leftOver = '\0';
 		}
+		else
+			leftOver = pair;
+Char.IsLetterOrDigit (pair);
 
 		// Return the final count to the caller.
 		return posn - byteIndex;
@@ -406,8 +422,12 @@ public class UTF8Encoding : Encoding
 
 		unsafe {
 			fixed (char* cptr = s) {
+				char dummy = '\0';
+				if (bytes.Length == byteIndex)
+					return InternalGetBytes (
+						cptr + charIndex, charCount,
+						null, 0, ref dummy, true);
 				fixed (byte *bptr = bytes) {
-					char dummy = '\0';
 					return InternalGetBytes (
 						cptr + charIndex, charCount,
 						bptr + byteIndex, bytes.Length - byteIndex,

+ 5 - 0
mcs/class/corlib/Test/System.Text/ChangeLog

@@ -1,3 +1,8 @@
+2006-02-03  Atsushi Enomoto  <[email protected]>
+
+	* UTF8EncodingTest.cs : added test for insufficient bytes for
+	  flush=true and leftOver!='\0' case.
+
 2006-01-24  Atsushi Enomoto  <[email protected]>
 
 	* UTF7EncodingTest.cs : added test for bug #77315.

+ 30 - 0
mcs/class/corlib/Test/System.Text/UTF8EncodingTest.cs

@@ -1016,5 +1016,35 @@ namespace MonoTests.System.Text {
 			new UTF8Encoding (false, true).GetString (
 				new byte [] {0xED, 0xA2, 0x8C});
 		}
+
+		[Test]
+		public void SufficientByteArray ()
+		{
+			Encoder e = Encoding.UTF8.GetEncoder ();
+			byte [] bytes = new byte [0];
+
+			char [] chars = new char [] {'\uD800'};
+			e.GetBytes (chars, 0, 1, bytes, 0, false);
+			try {
+				int ret = e.GetBytes (chars, 1, 0, bytes, 0, true);
+#if NET_2_0
+				AssertEquals ("drop insufficient char in 2.0: char[]", 0, ret);
+#else
+				Fail ("ArgumentException is expected: char[]");
+#endif
+			} catch (ArgumentException) {
+			}
+
+			string s = "\uD800";
+			try {
+				int ret = Encoding.UTF8.GetBytes (s, 0, 1, bytes, 0);
+#if NET_2_0
+				AssertEquals ("drop insufficient char in 2.0: string", 0, ret);
+#else
+				Fail ("ArgumentException is expected: string");
+#endif
+			} catch (ArgumentException) {
+			}
+		}
 	}
 }