[PATCH] Fix PR libgcj/36252, OutOfMemoryError in String constructor.
David Daney
ddaney@avtrex.com
Sun May 18 05:53:00 GMT 2008
This bug is caused by ambiguity in the return value of gnu.gcj.convert.BytesToUnicode.read(). A return value of zero indicates that no characters were placed in the output buffer. There are two possible causes:
1) The output buffer is too small.
2) The input buffer contains an incomplete code sequence.
In String(byte[] input_bytes, int offset, int count, String encoding) we were assuming that the zero return indicated that the output buffer was too small, so we would double its length and try again. If we pass input_bytes that ends in an incomplete code sequence, we end up looping forever allocating ever larger char[] until memory is exhausted.
My fix is to assume that the size of the largest atomic output sequence for *all* encodings is bounded and if the output buffer is larger than this bound, then there is a problem with the input and we should quit trying rather than expanding the size of the output buffer. I arbitrarily decided that this magic bound has a value less than or equal to 20. The reasoning being that stringing together several UTF16 encoded composing code points is the worse case and that this would certainly be smaller than 20 characters.
A bonus fix is to catch java.io.CharConversionException and stop the conversion. This keeps this checked exception from escaping from the constructor which does not declare that that it throws said exception.
Bootstrapped and tested on x86_64-pc-linux-gnu with no failures in libjava both on the trunk and 4.3 branch.
OK to commit to the trunk and branch?
2008-05-17 David Daney <ddaney@avtrex.com>
PR libgcj/36252
* java/lang/natString.ccn: Add
#include <java/io/CharConversionException.h>.
(init (byte[], int, int, String)): Catch and ignore
CharConversionException. Break out of conversion loop
on incomplete input.
* testsuite/libjava.lang/PR36252.java: New test.
* testsuite/libjava.lang/PR36252.out: New file, its expected output.
* testsuite/libjava.lang/PR36252.jar: New file, its pre-compiled
jar file.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PR36252.patch
Type: text/x-patch
Size: 3185 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/java-patches/attachments/20080518/6d876582/attachment.bin>
More information about the Java-patches
mailing list