logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rpo...@apache.org
Subject svn commit: r1617062 - in /logging/log4j/log4j2/trunk: log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/PatternParserTest.java src/changes/changes.xml
Date Sun, 10 Aug 2014 08:33:44 GMT
Author: rpopma
Date: Sun Aug 10 08:33:44 2014
New Revision: 1617062

URL: http://svn.apache.org/r1617062
Log:
LOG4J2-726: Prevent application from hanging when PatternLayout configuration has opening
'{' but no closing '}'.

Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/PatternParserTest.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java?rev=1617062&r1=1617061&r2=1617062&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java
Sun Aug 10 08:33:44 2014
@@ -252,7 +252,9 @@ public final class PatternParser {
             int depth = 0;
             do {
                 end = pattern.indexOf('}', i);
-                if (end != -1) {
+                if (end == -1) {
+                    break;
+                } else {
                     final int next = pattern.indexOf("{", i);
                     if (next != -1 && next < end) {
                         i = end + 1;

Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/PatternParserTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/PatternParserTest.java?rev=1617062&r1=1617061&r2=1617062&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/PatternParserTest.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/PatternParserTest.java
Sun Aug 10 08:33:44 2014
@@ -16,6 +16,7 @@
  */
 package org.apache.logging.log4j.core.pattern;
 
+import java.util.Calendar;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -51,6 +52,7 @@ public class PatternParserTest {
     private final String mdcMsgPattern4 = "%m : %X{key3}%n";
     private final String mdcMsgPattern5 = "%m : %X{key1},%X{key2},%X{key3}%n";
 
+    private static String badPattern = "[%d{yyyyMMdd HH:mm:ss,SSS] %-5p [%c{10}] - %m%n";
     private static String customPattern = "[%d{yyyyMMdd HH:mm:ss,SSS}] %-5p [%-25.25c{1}:%-4L]
- %m%n";
     private static String nestedPatternHighlight =
             "%highlight{%d{dd MMM yyyy HH:mm:ss,SSS}{GMT+0} [%t] %-5level: %msg%n%throwable}";
@@ -100,9 +102,34 @@ public class PatternParserTest {
             formatter.format(event, buf);
         }
         final String str = buf.toString();
-        final String expected = "INFO  [PatternParserTest        :93  ] - Hello, world" +
Constants.LINE_SEPARATOR;
+        final String expected = "INFO  [PatternParserTest        :95  ] - Hello, world" +
Constants.LINE_SEPARATOR;
         assertTrue("Expected to end with: " + expected + ". Actual: " + str, str.endsWith(expected));
     }
+    
+    @Test
+    public void testBadPattern() {
+        final Calendar cal = Calendar.getInstance();
+        cal.set(2001, Calendar.FEBRUARY, 3, 4, 5, 6);
+        cal.set(Calendar.MILLISECOND, 789);
+        final long timestamp = cal.getTimeInMillis();
+        
+        final List<PatternFormatter> formatters = parser.parse(badPattern);
+        assertNotNull(formatters);
+        final Throwable t = new Throwable();
+        final StackTraceElement[] elements = t.getStackTrace();
+        final LogEvent event = new Log4jLogEvent("a.b.c", null,
+            Logger.class.getName(), Level.INFO, new SimpleMessage("Hello, world"), null,
+            null, null, "Thread1", elements[0], timestamp);
+        final StringBuilder buf = new StringBuilder();
+        for (final PatternFormatter formatter : formatters) {
+            formatter.format(event, buf);
+        }
+        final String str = buf.toString();
+        
+        // eats all characters until the closing '}' character
+        final String expected = "[2001-02-03 04:05:06,789] - Hello, world";
+        assertTrue("Expected to start with: " + expected + ". Actual: " + str, str.startsWith(expected));
+    }
 
     @Test
     public void testNestedPatternHighlight() {

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1617062&r1=1617061&r2=1617062&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Sun Aug 10 08:33:44 2014
@@ -22,6 +22,9 @@
   </properties>
   <body>
     <release version="2.0.2" date="2014-0?-??" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-726" dev="rpopma" type="fix">
+        Prevent application from hanging when PatternLayout configuration has opening '{'
but no closing '}'.
+      </action>
       <action issue="LOG4J2-769" dev="rpopma" type="fix" due-to="Scott Harrington">
         Startup takes a long time if you have empty packages attribute.
       </action>



Mime
View raw message