This is the mail archive of the java-discuss@sourceware.cygnus.com 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]

serious bug in BytesToUnicode


My patch last week to BufferedReader (which fixed a problem with
losing characters during mark/reset operations) exposed a much
more serious bug in the gnu.gcj.convert input classes... this
bug causes an infinite loop in InputStreamReader's read(char[])
method, which causes BufferedReader to hang forever if trying to
do a readLine() when it needs to refill the buffer.

The bug is the the convert() call in BytesToUnicode is documented
to take 3 parameters:  buffer, offset, and length.  Length is the
number of characters to read, just like in the Reader calls (as
the author helpfully documented in the javadocs).  In all (?) the
gnu.gcj.convert classes extending BytesToUnicode, the length parameter
is assumed to be the total length of the buffer, which causes them
to screw up when offset is nonzero.  Since the call from InputStreamReader
assumes the documented behavior, I patched Input_8859_1.java to
conform to this... this fixes the bug on my system.  However,
in looking through the other Input_*.java classes, I noticed this
same bug in several others.  Therefore, these classes also need to be
fixed to prevent this bug from appearing with other encoding systems, or
the convert() API should be changed and InputStreamReader (and
probably other classes) needs to be fixed to conform to the new API.

Here is my patch... I would send these to java-patches, but I haven't
seen any traffic there yet.  Also, if Cygnus needs me to sign over
the changes, let me know; everything submitted so far has been only
1 or 2 lines.

diff -u -r libgcj-snapshot-1999-06-18.orig/libjava/gnu/gcj/convert/Input_8859_1.java libgcj-snapshot-1999-06-18/libjava/gnu/gcj/convert/Input_8859_1.java
--- libgcj-snapshot-1999-06-18.orig/libjava/gnu/gcj/convert/Input_8859_1.java
Tue Jun 29 16:00:48 1999
+++ libgcj-snapshot-1999-06-18/libjava/gnu/gcj/convert/Input_8859_1.java
Tue Jun 29 15:58:46 1999
@@ -25,7 +25,8 @@
     int inpos = this.inpos;
     byte[] inbuffer = this.inbuffer;
     int inavail = this.inlength - inpos;
-    int outavail = outlength - outpos;
+    int outavail = outlength;
+    // NOT: outavail = outlength - outpos, at least according to the docs!
     if (outavail > inavail)
       outavail = inavail;
     while (--outavail >= 0)

JMC
-- 
John-Marc Chandonia (jmc@cmpharm.ucsf.edu)              We're everywhere...
Cohen Lab, University of California San Francisco       for your convenience.
http://yuri.harvard.edu/~jmc                                -- Psi Corps <*>

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