This is an automated email from the ASF dual-hosted git repository. chibenwa pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-mime4j.git
commit ac88320f72615acd8fb0b92c11c9a1ff9be94710 Author: Benoit TELLIER <[email protected]> AuthorDate: Tue Dec 1 21:05:02 2026 +0100 [FIX] TextInputStream: grow the byte buffer that cannot hold the bytes of one char With a byte buffer smaller than the bytes of the next char (a 3-byte UTF-8 char in 2 bytes) or than the final escape sequence of a stateful charset, encode / flush returned OVERFLOW without writing anything, fillBuffer left the buffer empty and read() called it again forever. The buffer is now doubled when an OVERFLOW wrote nothing, and the call is retried. Only the package-private constructor could reach it: InputStreams passes 1024 bytes. --- .../org/apache/james/mime4j/io/TextInputStream.java | 21 +++++++++++++++++++-- .../apache/james/mime4j/io/TextInputStreamTest.java | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/apache/james/mime4j/io/TextInputStream.java b/core/src/main/java/org/apache/james/mime4j/io/TextInputStream.java index 03611731..8bcc770f 100644 --- a/core/src/main/java/org/apache/james/mime4j/io/TextInputStream.java +++ b/core/src/main/java/org/apache/james/mime4j/io/TextInputStream.java @@ -48,7 +48,8 @@ class TextInputStream extends InputStream { private final CharBuffer cbuf; // index in text of the first char not copied into the window private int textPos; - private final ByteBuffer bbuf; + // grown when it cannot hold the bytes of a single char, or the end of a stateful encoding + private ByteBuffer bbuf; private int mark = -1; // encode(..., true) consumed all the input @@ -113,6 +114,11 @@ class TextInputStream extends InputStream { result.throwException(); } if (!result.isUnderflow()) { + if (this.bbuf.position() == 0) { + // nothing written: the buffer cannot hold the bytes of the next char + growBuffer(); + continue; + } // the byte buffer is full break; } @@ -123,16 +129,27 @@ class TextInputStream extends InputStream { refillWindow(); } } - if (this.encoded && !this.flushed) { + while (this.encoded && !this.flushed) { CoderResult result = this.encoder.flush(this.bbuf); if (result.isError()) { result.throwException(); } this.flushed = result.isUnderflow(); + if (!this.flushed && this.bbuf.position() == 0) { + // nothing written: the buffer cannot hold the final bytes + growBuffer(); + continue; + } + break; } this.bbuf.flip(); } + // Called in write mode with nothing written: no byte is lost + private void growBuffer() { + this.bbuf = ByteBuffer.allocate(Math.max(8, this.bbuf.capacity() * 2)); + } + private boolean atEnd() { return !this.bbuf.hasRemaining() && this.flushed; } diff --git a/core/src/test/java/org/apache/james/mime4j/io/TextInputStreamTest.java b/core/src/test/java/org/apache/james/mime4j/io/TextInputStreamTest.java index 9e4f33ad..0dc50a1b 100644 --- a/core/src/test/java/org/apache/james/mime4j/io/TextInputStreamTest.java +++ b/core/src/test/java/org/apache/james/mime4j/io/TextInputStreamTest.java @@ -248,6 +248,25 @@ public class TextInputStreamTest { Assert.assertEquals(0, in.skip(10)); } + // A buffer smaller than the bytes of one char made read() loop forever + @Test(timeout = 10000) + public void shouldEncodeACharLargerThanTheBuffer() throws Exception { + String text = "a\u20ac\ud83d\ude00b"; + Assert.assertArrayEquals(text.getBytes(Charsets.UTF_8), drain(new TextInputStream(text, Charsets.UTF_8, 1))); + + InputStream in = new TextInputStream(text, Charsets.UTF_8, 2); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + int b; + while ((b = in.read()) != -1) { + out.write(b); + } + Assert.assertArrayEquals(text.getBytes(Charsets.UTF_8), out.toByteArray()); + + Charset iso2022jp = Charset.forName("ISO-2022-JP"); + String kanji = "\u3053\u3093"; + Assert.assertArrayEquals(kanji.getBytes(iso2022jp), drain(new TextInputStream(kanji, iso2022jp, 1))); + } + private static byte[] drain(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[100]; --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
