You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2016/06/03 16:29:42 UTC

logging-log4j2 git commit: LOG4J2-1409 Fixed ArrayIndexOutOfBoundsException that may occur in ReusableParameterizedMessage.

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 448dba885 -> 6d80ddf7e


LOG4J2-1409 Fixed ArrayIndexOutOfBoundsException that may occur in ReusableParameterizedMessage.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/6d80ddf7
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/6d80ddf7
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/6d80ddf7

Branch: refs/heads/master
Commit: 6d80ddf7ed92d65174b52fe946ae73d74f437d8b
Parents: 448dba8
Author: rpopma <rp...@apache.org>
Authored: Sat Jun 4 01:30:33 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Sat Jun 4 01:30:33 2016 +0900

----------------------------------------------------------------------
 .../message/ReusableParameterizedMessage.java   | 29 +++++++++--
 ...MutableLogEventWithReusableParamMsgTest.java | 53 ++++++++++++++++++++
 src/changes/changes.xml                         |  3 ++
 3 files changed, 81 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6d80ddf7/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableParameterizedMessage.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableParameterizedMessage.java b/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableParameterizedMessage.java
index 24c0e7d..abfd620 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableParameterizedMessage.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/ReusableParameterizedMessage.java
@@ -17,7 +17,6 @@
 package org.apache.logging.log4j.message;
 
 import java.util.Arrays;
-import java.util.Objects;
 
 import org.apache.logging.log4j.util.PerformanceSensitive;
 
@@ -62,10 +61,32 @@ public class ReusableParameterizedMessage implements ReusableMessage {
         Object[] result;
         if (varargs == null) {
             result = params;
-            params = Objects.requireNonNull(emptyReplacement);
+            if (emptyReplacement.length >= 10) {
+                params = emptyReplacement;
+            } else {
+                // Bad replacement! Too small, may blow up future 10-arg messages.
+                if (argCount <= emptyReplacement.length) {
+                    // copy params into the specified replacement array and return that
+                    System.arraycopy(params, 0, emptyReplacement, 0, argCount);
+                    result = emptyReplacement;
+                } else {
+                    // replacement array is too small for current content and future content: discard it
+                    params = new Object[10];
+                }
+            }
         } else {
-            result = varargs;
-            varargs = Objects.requireNonNull(emptyReplacement);
+            // The returned array will be reused by the caller in future swapParameter() calls.
+            // Therefore we want to avoid returning arrays with less than 10 elements.
+            // If the vararg array is less than 10 params we just copy its content into the specified array
+            // and return it. This helps the caller to retain a reusable array of at least 10 elements.
+            if (argCount <= emptyReplacement.length) {
+                // copy params into the specified replacement array and return that
+                System.arraycopy(varargs, 0, emptyReplacement, 0, argCount);
+                result = emptyReplacement;
+            } else {
+                result = varargs;
+                varargs = emptyReplacement;
+            }
         }
         return result;
     }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6d80ddf7/log4j-core/src/test/java/org/apache/logging/log4j/message/MutableLogEventWithReusableParamMsgTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/message/MutableLogEventWithReusableParamMsgTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/message/MutableLogEventWithReusableParamMsgTest.java
new file mode 100644
index 0000000..6e50c01
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/message/MutableLogEventWithReusableParamMsgTest.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.message;
+
+import org.apache.logging.log4j.core.impl.MutableLogEvent;
+import org.junit.Test;
+
+/**
+ * LOG4J2-1409
+ */
+// test must be in log4j-core but in org.apache.logging.log4j.message package because it calls package-private methods
+public class MutableLogEventWithReusableParamMsgTest {
+    @Test
+    public void testInteractionWithReusableParameterizedMessage() {
+        MutableLogEvent evt = new MutableLogEvent();
+        ReusableParameterizedMessage msg = new ReusableParameterizedMessage();
+        msg.set("Hello {} {} {}", 1, 2, 3);
+        evt.setMessage(msg);
+        evt.clear();
+
+        msg.set("Hello {}", new Object[]{1});
+        evt.setMessage(msg);
+        evt.clear();
+
+        msg.set("Hello {}", 1);
+        evt.setMessage(msg);
+        evt.clear();
+
+        // Uncomment out this log event and the params gets reset correctly (No exception occurs)
+        //        msg.set("Hello {}", 1);
+        //        evt.setMessage(msg);
+        //        evt.clear();
+
+        // Exception at this log event - as the params is set to 1!
+        msg.set("Hello {} {} {}", 1, 2, 3);
+        evt.setMessage(msg);
+        evt.clear();
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/6d80ddf7/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a0d1493..3b4188f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -24,6 +24,9 @@
   </properties>
   <body>
     <release version="2.6.1" date="2016-MM-DD" description="GA Release 2.6.1">
+      <action issue="LOG4J2-1409" dev="rpopma" type="fix" due-to="Shahan">
+        Fixed ArrayIndexOutOfBoundsException that may occur in ReusableParameterizedMessage.
+      </action>
       <action issue="LOG4J2-997" dev="rgoers" type="fix" due-to="Maytee Chinavanichkit">
         Add filter and remove filter were not working properly in AbstractFilterable.
       </action>