|
| 1 | +From 124575963f02242904d655cfde7d05375c2c2d26 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Razvan-Daniel Mihai < [email protected]> |
| 3 | +Date: Wed, 5 Nov 2025 12:27:34 +0100 |
| 4 | +Subject: fix: move buffer into function to prevent race |
| 5 | + |
| 6 | +--- |
| 7 | + src/main/java/org/apache/log4j/helpers/Transform.java | 4 ++-- |
| 8 | + src/main/java/org/apache/log4j/xml/XMLLayout.java | 10 +--------- |
| 9 | + 2 files changed, 3 insertions(+), 11 deletions(-) |
| 10 | + |
| 11 | +diff --git a/src/main/java/org/apache/log4j/helpers/Transform.java b/src/main/java/org/apache/log4j/helpers/Transform.java |
| 12 | +index ad77e165..5f0aba25 100644 |
| 13 | +--- a/src/main/java/org/apache/log4j/helpers/Transform.java |
| 14 | ++++ b/src/main/java/org/apache/log4j/helpers/Transform.java |
| 15 | +@@ -76,11 +76,11 @@ public class Transform { |
| 16 | + /** |
| 17 | + * Ensures that embeded CDEnd strings (]]>) are handled properly within message, NDC and throwable tag text. |
| 18 | + * |
| 19 | +- * @param buf StringBuffer holding the XML data to this point. The initial CDStart (<![CDATA[) and final CDEnd |
| 20 | ++ * @param buf StringBuilder holding the XML data to this point. The initial CDStart (<![CDATA[) and final CDEnd |
| 21 | + * (]]>) of the CDATA section are the responsibility of the calling method. |
| 22 | + * @param str The String that is inserted into an existing CDATA Section within buf. |
| 23 | + */ |
| 24 | +- static public void appendEscapingCDATA(final StringBuffer buf, final String str) { |
| 25 | ++ static public void appendEscapingCDATA(final StringBuilder buf, final String str) { |
| 26 | + if (str != null) { |
| 27 | + int end = str.indexOf(CDATA_END); |
| 28 | + if (end < 0) { |
| 29 | +diff --git a/src/main/java/org/apache/log4j/xml/XMLLayout.java b/src/main/java/org/apache/log4j/xml/XMLLayout.java |
| 30 | +index 4f0d99f3..35ef30d8 100644 |
| 31 | +--- a/src/main/java/org/apache/log4j/xml/XMLLayout.java |
| 32 | ++++ b/src/main/java/org/apache/log4j/xml/XMLLayout.java |
| 33 | +@@ -64,9 +64,7 @@ import java.util.Arrays; |
| 34 | + public class XMLLayout extends Layout { |
| 35 | + |
| 36 | + private final int DEFAULT_SIZE = 256; |
| 37 | +- private final int UPPER_LIMIT = 2048; |
| 38 | + |
| 39 | +- private StringBuffer buf = new StringBuffer(DEFAULT_SIZE); |
| 40 | + private boolean locationInfo = false; |
| 41 | + private boolean properties = false; |
| 42 | + |
| 43 | +@@ -121,13 +119,7 @@ public class XMLLayout extends Layout { |
| 44 | + */ |
| 45 | + public String format(final LoggingEvent event) { |
| 46 | + |
| 47 | +- // Reset working buffer. If the buffer is too large, then we need a new |
| 48 | +- // one in order to avoid the penalty of creating a large array. |
| 49 | +- if (buf.capacity() > UPPER_LIMIT) { |
| 50 | +- buf = new StringBuffer(DEFAULT_SIZE); |
| 51 | +- } else { |
| 52 | +- buf.setLength(0); |
| 53 | +- } |
| 54 | ++ StringBuilder buf = new StringBuilder(DEFAULT_SIZE); |
| 55 | + |
| 56 | + // We yield to the \r\n heresy. |
| 57 | + |
0 commit comments