This is an automated email from the ASF dual-hosted git repository. rmaucher pushed a commit to branch 9.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit f1ce5cbd7cb2b155bb67d843483cd15fc82ab813 Author: opencode <[email protected]> AuthorDate: Wed Oct 7 17:32:24 2026 +0200 Use long arithmetic when calculating session rates in ManagerBase calculateRate() cast the millisecond span covered by the timing cache to int. On applications sparse enough for the cache to span more than 2^31 milliseconds, the cast overflowed and the JMX rate attributes were reported as negative values or, when the truncated span was zero, the division threw an ArithmeticException. Perform the calculation in long arithmetic. --- java/org/apache/catalina/session/ManagerBase.java | 5 +- .../apache/catalina/session/TestManagerBase.java | 63 ++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/java/org/apache/catalina/session/ManagerBase.java b/java/org/apache/catalina/session/ManagerBase.java index 949382e642..5277bd9791 100644 --- a/java/org/apache/catalina/session/ManagerBase.java +++ b/java/org/apache/catalina/session/ManagerBase.java @@ -1172,8 +1172,9 @@ public abstract class ManagerBase extends LifecycleMBeanBase implements Manager } } if (counter > 0) { - if (oldest < now) { - result = (1000 * 60 * counter) / (int) (now - oldest); + long span = now - oldest; + if (span > 0) { + result = (int) ((1000L * 60 * counter) / span); } else { // Better than reporting zero result = Integer.MAX_VALUE; diff --git a/test/org/apache/catalina/session/TestManagerBase.java b/test/org/apache/catalina/session/TestManagerBase.java new file mode 100644 index 0000000000..82f59ba48c --- /dev/null +++ b/test/org/apache/catalina/session/TestManagerBase.java @@ -0,0 +1,63 @@ +/* + * 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.catalina.session; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.catalina.session.ManagerBase.SessionTiming; + +public class TestManagerBase { + + /* + * Timing entries more than 2^31 milliseconds old must produce a sensible + * rate rather than the result of an overflowing cast to int, which could + * be negative or lead to a division by zero. + */ + @Test + public void testSessionRateWithSpanOverIntMax() { + StandardManager manager = new StandardManager(); + + long now = System.currentTimeMillis(); + + manager.sessionCreationTiming.clear(); + manager.sessionCreationTiming.add(new SessionTiming(now - (1L << 32), 100)); + + // Before the fix the delta was cast to int, yielding zero (division by + // zero) or a small garbage value for spans that are a multiple of + // 2^32 + Assert.assertEquals(0, manager.getSessionCreateRate()); + } + + @Test + public void testSessionRateWithSpanOverflowingToNegative() { + StandardManager manager = new StandardManager(); + + long now = System.currentTimeMillis(); + + // A span just under 2^32 casts to a negative int + long span = (1L << 32) - 3000; + + manager.sessionCreationTiming.clear(); + for (int i = 0; i < 100; i++) { + manager.sessionCreationTiming.add(new SessionTiming(now - span, 100)); + } + + // Before the fix this returned a negative rate + Assert.assertEquals(0, manager.getSessionCreateRate()); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
