Repository: logging-log4j2
Updated Branches:
  refs/heads/master fa2f15022 -> b13c4e0d8


[LOG4J2-980] Numerical overflow in BurstFilter not handled correctly.

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

Branch: refs/heads/master
Commit: b13c4e0d86629d12437b186721766087e851fbff
Parents: fa2f150
Author: Gary Gregory <[email protected]>
Authored: Sat Mar 21 09:57:32 2015 -0700
Committer: Gary Gregory <[email protected]>
Committed: Sat Mar 21 09:57:32 2015 -0700

----------------------------------------------------------------------
 .../logging/log4j/core/filter/BurstFilter.java  | 25 ++++++-----
 .../core/filter/BurstFilterLogDelayTest.java    | 45 ++++++++++++++++++++
 src/changes/changes.xml                         |  3 ++
 3 files changed, 62 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b13c4e0d/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/BurstFilter.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/BurstFilter.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/BurstFilter.java
index 49190a2..f0ee65c 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/BurstFilter.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/BurstFilter.java
@@ -81,13 +81,17 @@ public final class BurstFilter extends AbstractFilter {
 
     private final Queue<LogDelay> available = new 
ConcurrentLinkedQueue<LogDelay>();
 
+    static LogDelay createLogDelay(long expireTime) {
+        return new LogDelay(expireTime);
+    }
+    
     private BurstFilter(final Level level, final float rate, final long 
maxBurst, final Result onMatch,
                         final Result onMismatch) {
         super(onMatch, onMismatch);
         this.level = level;
         this.burstInterval = (long) (NANOS_IN_SECONDS * (maxBurst / rate));
         for (int i = 0; i < maxBurst; ++i) {
-            available.add(new LogDelay());
+            available.add(createLogDelay(0));
         }
     }
 
@@ -167,14 +171,17 @@ public final class BurstFilter extends AbstractFilter {
 
     /**
      * Delay object to represent each log event that has occurred within the 
timespan.
+     * 
+     * Consider this class private, package visibility for testing.
      */
-    private class LogDelay implements Delayed {
-
-        private long expireTime;
+    private static class LogDelay implements Delayed {
 
-        public LogDelay() {
+        LogDelay(long expireTime) {
+            this.expireTime = expireTime;
         }
 
+        private long expireTime;
+
         public void setDelay(final long delay) {
             this.expireTime = delay + System.nanoTime();
         }
@@ -186,12 +193,8 @@ public final class BurstFilter extends AbstractFilter {
 
         @Override
         public int compareTo(final Delayed delayed) {
-            if (this.expireTime < ((LogDelay) delayed).expireTime) {
-                return -1;
-            } else if (this.expireTime > ((LogDelay) delayed).expireTime) {
-                return 1;
-            }
-            return 0;
+            final long diff = this.expireTime - ((LogDelay) 
delayed).expireTime;
+            return Long.compare(diff, 0);
         }
 
         @Override

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b13c4e0d/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/BurstFilterLogDelayTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/BurstFilterLogDelayTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/BurstFilterLogDelayTest.java
new file mode 100644
index 0000000..f3a46ad
--- /dev/null
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/BurstFilterLogDelayTest.java
@@ -0,0 +1,45 @@
+/* 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.core.filter;
+
+import static org.hamcrest.Matchers.greaterThan;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+import java.util.concurrent.Delayed;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Test;
+
+/**
+ * Unit test for <code>BurstFilter</code>.
+ */
+public class BurstFilterLogDelayTest {
+
+    @Test
+    public void testCompareToOverflow() {
+        // no overflow, but close
+        Delayed d1 = BurstFilter.createLogDelay(Long.MAX_VALUE - 
TimeUnit.SECONDS.toNanos(10) - System.nanoTime());
+
+        // Overflow
+        Delayed d2 = BurstFilter.createLogDelay(Long.MAX_VALUE + 
TimeUnit.SECONDS.toNanos(10) - System.nanoTime());
+
+        assertThat(d2, is(greaterThan(d1)));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b13c4e0d/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index cc45bbb..6ae9dba 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -27,6 +27,9 @@
       <action issue="LOG4J2-926" dev="ggregory" type="add" due-to="David 
Ohana">
         Truncate from the end of text format modifier.
       </action>
+      <action issue="LOG4J2-980" dev="ggregory" type="fix" due-to="Mikhail 
Mazurskiy">
+        Numerical overflow in BurstFilter not handled correctly.
+      </action>
       <action issue="LOG4J2-966" dev="ggregory" type="fix" due-to="Gary 
Gregory">
         KeyStoreConfiguration.createKeyStoreConfiguration() ignores 
keyManagerFactoryAlgorithm.
       </action>

Reply via email to