This is an automated email from the ASF dual-hosted git repository.

markt-asf pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/10.1.x by this push:
     new c730f5cdb1 Review "Reject bucket durations that overflow the shift 
based time ..."
c730f5cdb1 is described below

commit c730f5cdb110dc1fccc0641bf8a45e6902436c1e
Author: Mark Thomas <[email protected]>
AuthorDate: Fri Oct 23 22:26:38 2026 +0100

    Review "Reject bucket durations that overflow the shift based time ..."
    
    Document the limit.
    Reject negative values
---
 java/org/apache/catalina/util/TimeBucketCounter.java     | 8 ++++++--
 test/org/apache/catalina/util/TestTimeBucketCounter.java | 7 +++++++
 webapps/docs/config/filter.xml                           | 7 +++++--
 3 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/java/org/apache/catalina/util/TimeBucketCounter.java 
b/java/org/apache/catalina/util/TimeBucketCounter.java
index 4282840d83..a1ad709c43 100644
--- a/java/org/apache/catalina/util/TimeBucketCounter.java
+++ b/java/org/apache/catalina/util/TimeBucketCounter.java
@@ -97,10 +97,14 @@ public class TimeBucketCounter extends 
TimeBucketCounterBase {
      *
      * @return bits to be shifted
      *
-     * @throws IllegalArgumentException if the duration in milliseconds, 
rounded up to the next power of 2, would not
-     *                                      fit in an int
+     * @throws IllegalArgumentException if the duration is not positive, or if 
the duration in milliseconds, rounded up
+     *                                      to the next power of 2, would not 
fit in an int
      */
     protected static int determineShiftBitsOfDuration(int duration) {
+        if (duration <= 0) {
+            throw new IllegalArgumentException(
+                    sm.getString("timebucket.invalidDuration", 
Integer.valueOf(duration)));
+        }
         if (duration > MAX_DURATION_SECONDS) {
             // duration * 1000, rounded up to the next power of 2, would 
overflow int. Reject it with a clear
             // message rather than computing a meaningless number of bits.
diff --git a/test/org/apache/catalina/util/TestTimeBucketCounter.java 
b/test/org/apache/catalina/util/TestTimeBucketCounter.java
index ed385a900f..f24e282712 100644
--- a/test/org/apache/catalina/util/TestTimeBucketCounter.java
+++ b/test/org/apache/catalina/util/TestTimeBucketCounter.java
@@ -77,6 +77,13 @@ public class TestTimeBucketCounter {
                 new java.util.concurrent.ScheduledThreadPoolExecutor(1)));
     }
 
+    @Test
+    public void testNegativeDurationAfterWrapAround() {
+        // Previously this wrapped to 1073741296 milliseconds and produced a 
valid 2^30 millisecond bucket
+        Assert.assertThrows(IllegalArgumentException.class, () -> new 
TimeBucketCounter(-3221226,
+                new java.util.concurrent.ScheduledThreadPoolExecutor(1)));
+    }
+
     @Test
     public void testGetMillisUntilNextBucket() throws InterruptedException {
         long millis;
diff --git a/webapps/docs/config/filter.xml b/webapps/docs/config/filter.xml
index ef53e37abc..3465e25ca3 100644
--- a/webapps/docs/config/filter.xml
+++ b/webapps/docs/config/filter.xml
@@ -1089,11 +1089,14 @@ FINE: Request "/docs/config/manager.html" with response 
status "200"
     using bit shift arithmetic. In order to remain true to the user intent, the
     configured number of requests is then multiplied by the same ratio, so a
     configuration of 100 Requests per 60 seconds, has the real values of 109
-    Requests per 65 seconds. An alternative implementation,
+    Requests per 65 seconds. Because of this conversion, the default
+    implementation rejects a configured duration of more than 1073741 seconds
+    (about 12 days). An alternative implementation,
     <code>org.apache.catalina.util.ExactRateLimiter</code>, is intended to
     provide a less efficient but more accurate control, whose effective 
duration
     in seconds and number of requests allowed are consist with the configured
-    values. You can specify a different class as long as it implements the
+    values and does not have that limit on the duration. You can specify a
+    different class as long as it implements the
     <code>org.apache.catalina.util.RateLimiter</code> interface.</p>
 
     <p>It is common to set up different restrictions for different URIs.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to