From c99210d981ded42ab7573b607a09981586a7e403 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Wed, 12 Feb 2020 10:48:15 -0800 Subject: [PATCH] Fix single newline in logging output stream buffer (#52253) The buffer in LoggingOutputStream skips flushing when only a newline appears. However, if a windows newline appeared, the buffer length was not reset. This commit resets the length so the \r does not appear in the next logging message. closes #51838 --- .../org/elasticsearch/common/logging/LoggingOutputStream.java | 1 + .../elasticsearch/common/logging/LoggingOutputStreamTests.java | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/org/elasticsearch/common/logging/LoggingOutputStream.java b/server/src/main/java/org/elasticsearch/common/logging/LoggingOutputStream.java index 54af0a1ccfdf0..e9fa9f737143b 100644 --- a/server/src/main/java/org/elasticsearch/common/logging/LoggingOutputStream.java +++ b/server/src/main/java/org/elasticsearch/common/logging/LoggingOutputStream.java @@ -98,6 +98,7 @@ public void flush() { } if (used == 0) { // only windows \r was in the buffer + buffer.used = 0; return; } log(new String(buffer.bytes, 0, used, StandardCharsets.UTF_8)); diff --git a/server/src/test/java/org/elasticsearch/common/logging/LoggingOutputStreamTests.java b/server/src/test/java/org/elasticsearch/common/logging/LoggingOutputStreamTests.java index f314144c4212d..187e1b13a80ed 100644 --- a/server/src/test/java/org/elasticsearch/common/logging/LoggingOutputStreamTests.java +++ b/server/src/test/java/org/elasticsearch/common/logging/LoggingOutputStreamTests.java @@ -81,6 +81,7 @@ public void testNull() { // this test explicitly outputs the newlines instead of relying on println, to always test the unix behavior public void testFlushOnUnixNewline() { printStream.print("hello\n"); + printStream.print("\n"); // newline by itself does not show up printStream.print("world\n"); assertThat(loggingStream.lines, contains("hello", "world")); } @@ -88,6 +89,7 @@ public void testFlushOnUnixNewline() { // this test explicitly outputs the newlines instead of relying on println, to always test the windows behavior public void testFlushOnWindowsNewline() { printStream.print("hello\r\n"); + printStream.print("\r\n"); // newline by itself does not show up printStream.print("world\r\n"); assertThat(loggingStream.lines, contains("hello", "world")); } @@ -102,7 +104,6 @@ public void testBufferExtension() { assertThat(loggingStream.threadLocal.get().bytes.length, equalTo(DEFAULT_BUFFER_LENGTH)); } - @AwaitsFix( bugUrl = "https://github.com/elastic/elasticsearch/issues/51838") public void testMaxBuffer() { String longStr = randomAlphaOfLength(MAX_BUFFER_LENGTH); String extraLongStr = longStr + "OVERFLOW";