This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch: FYI: PR 27171


I'm checking this in on the trunk and the 4.1 branch.

This fixes some bugs in the UTF-8 generator, including PR 27171:

* We weren't properly handling an isolated low surrogate
* We weren't properly handling an isolated high surrogate at the end
  of the stream (in a few different places)

Tom

Index: ChangeLog
from  Tom Tromey  <tromey@redhat.com>

	PR libgcj/27171:
	* testsuite/libjava.lang/pr27171.java: New file.
	* testsuite/libjava.lang/pr27171.out: New file.
	* gnu/gcj/convert/Output_UTF8.java (havePendingBytes): Return
	true if we've seen a high surrogate.
	(write): Handle high surrogates at the end of the stream.
	Properly emit isolated low surrogates.

Index: gnu/gcj/convert/Output_UTF8.java
===================================================================
--- gnu/gcj/convert/Output_UTF8.java	(revision 112924)
+++ gnu/gcj/convert/Output_UTF8.java	(working copy)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1999, 2000, 2003  Free Software Foundation
+/* Copyright (C) 1999, 2000, 2003, 2006  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -36,7 +36,7 @@
     int avail = buf.length - count;
     for (;;)
       {
-	if (avail == 0 || (inlength == 0 && bytes_todo == 0))
+	if (avail == 0 || (inlength == 0 && bytes_todo == 0 && hi_part == 0))
 	  break;
 	// The algorithm is made more complicated because we want to write
 	// at least one byte in the output buffer, if there is room for
@@ -61,17 +61,25 @@
 	    continue;
 	  }
 
+	// Handle a high surrogate at the end of the input stream.
+	if (inlength == 0 && hi_part != 0)
+	  {
+	    buf[count++] = (byte) (0xE0 | (hi_part >> 12));
+	    value = hi_part;
+	    hi_part = 0;
+	    avail--;
+	    bytes_todo = 2;
+	    continue;
+	  }
+
 	char ch = inbuffer[inpos++];
 	inlength--;
 
-	if ((hi_part != 0 && (ch <= 0xDBFF || ch > 0xDFFF))
-	    || (hi_part == 0 && ch >= 0xDC00 && ch <= 0xDFFF))
+	if (hi_part != 0 && (ch <= 0xDBFF || ch > 0xDFFF))
 	  {
 	    // If the previous character was a high surrogate, and we
 	    // don't now have a low surrogate, we print the high
-	    // surrogate as an isolated character.  If this character
-	    // is a low surrogate and we didn't previously see a high
-	    // surrogate, we do the same thing.
+	    // surrogate as an isolated character.
 	    --inpos;
 	    ++inlength;
 	    buf[count++] = (byte) (0xE0 | (hi_part >> 12));
@@ -80,6 +88,16 @@
 	    avail--;
 	    bytes_todo = 2;
 	  }
+	else if (hi_part == 0 && ch >= 0xDC00 && ch <= 0xDFFF)
+	  {
+	    // If this character is a low surrogate and we didn't
+	    // previously see a high surrogate, we do the same thing
+	    // as above.
+	    buf[count++] = (byte) (0xE0 | (ch >> 12));
+	    value = ch;
+	    avail--;
+	    bytes_todo = 2;
+	  }
 	else if (ch < 128 && (ch != 0 || standardUTF8))
 	  {
 	    avail--;
@@ -122,7 +140,7 @@
 
   public boolean havePendingBytes()
   {
-    return bytes_todo > 0;
+    return bytes_todo > 0 || hi_part != 0;
   }
 
 }
Index: testsuite/libjava.lang/pr27171.out
===================================================================
--- testsuite/libjava.lang/pr27171.out	(revision 0)
+++ testsuite/libjava.lang/pr27171.out	(revision 0)
@@ -0,0 +1,2 @@
+3
+3
Index: testsuite/libjava.lang/pr27171.java
===================================================================
--- testsuite/libjava.lang/pr27171.java	(revision 0)
+++ testsuite/libjava.lang/pr27171.java	(revision 0)
@@ -0,0 +1,19 @@
+public class pr27171 {
+
+	public static void main(String[] args) throws Throwable {
+	  // Isolated low surrogate.
+	  char x = 56478;  // 0xdc9e
+	  String xs = new String(new char[] { x });
+	  // Note that we fix a result for our implementation; but
+	  // the JDK does something else.
+	  System.out.println(xs.getBytes("UTF-8").length);
+
+	  // isolated high surrogate -- at end of input stream
+	  char y = 0xdaee;
+	  String ys = new String(new char[] { y });
+	  // Note that we fix a result for our implementation; but
+	  // the JDK does something else.
+	  System.out.println(ys.getBytes("UTF-8").length);
+	}
+}
+


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]