From 4fe95e7fd6923b6fa740370d972aaeabe43f2f4e Mon Sep 17 00:00:00 2001 From: Flavia Rainone Date: Tue, 15 Oct 2024 18:21:46 -0300 Subject: [PATCH] [UNDERTOW-2448] At ServletPrintWriter.write(CharBuffer) do not mark error if buffer.remaining() == remaining after flush. We need to update remaining to do a proper check here. The aforementioned == check can return true after a successfull flush, because "remaining" value is set to be the size of the bytes remaining in the buffer before encoding. If, at that stage, the buffer is empty, "remaining" value is set to buffer.capacity(). When encoding many bytes, we will end up with a full buffer. As a result, we will try to flush, and it is often the case that the buffer will be entirely cleared after flushing. This results in buffer.remaining() being the overall buffer size, i.e., buffer.capacity(). As a result, buffer.remaining() == remaining check returns true, and write is aborted, causing the broken responses we are seeing. Signed-off-by: Flavia Rainone --- .../java/io/undertow/servlet/spec/ServletPrintWriter.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/servlet/src/main/java/io/undertow/servlet/spec/ServletPrintWriter.java b/servlet/src/main/java/io/undertow/servlet/spec/ServletPrintWriter.java index 49609f7dd0..64b426c9e0 100644 --- a/servlet/src/main/java/io/undertow/servlet/spec/ServletPrintWriter.java +++ b/servlet/src/main/java/io/undertow/servlet/spec/ServletPrintWriter.java @@ -180,8 +180,10 @@ public void write(final CharBuffer input) { remainingContentLength -= writtenLength; outputStream.updateWritten(writtenLength); if (result.isOverflow() || !buffer.hasRemaining()) { + final int remainingBytesBeforeFlush = buffer.remaining(); outputStream.flushInternal(); - if (buffer.remaining() == remaining) { + if (buffer.remaining() == remainingBytesBeforeFlush) { + // no progress has been made, set error to true error = true; return; }