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

zrhoffman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new f48986532e Fix integer overflow for czCount (#7272)
f48986532e is described below

commit f48986532e7bea8ea20ad0e1f64c3f9aa5849e05
Author: Srijeet Chatterjee <[email protected]>
AuthorDate: Mon Jan 9 12:01:16 2023 -0700

    Fix integer overflow for czCount (#7272)
    
    * Fix integer overflow for czCount
    
    * code review
    
    * fix changelog, test message
---
 CHANGELOG.md                                       |  1 +
 .../traffic_router/core/router/StatTracker.java    | 13 ++++---
 .../core/router/StatTrackerTest.java               | 43 ++++++++++++++++++++++
 3 files changed, 52 insertions(+), 5 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index c1fbf13ab2..a266f123b1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -36,6 +36,7 @@ The format is based on [Keep a 
Changelog](http://keepachangelog.com/en/1.0.0/).
 - [#7120](https://github.com/apache/trafficcontrol/pull/7120) *Docs* Update 
t3c documentation regarding parent.config parent_retry.
 
 ### Fixed
+- [#7252](https://github.com/apache/trafficcontrol/issues/7252) *Traffic 
Router* Fixed integer overflow for `czCount`, by resetting the count to max 
value when it overflows.
 - [#7221](https://github.com/apache/trafficcontrol/issues/7221) *Docs* Fixed 
request structure spec in cdn locks description in APIv4.
 - [#7225](https://github.com/apache/trafficcontrol/issues/7225) *Docs* Fixed 
docs for /roles response description in APIv4.
 - [#7246](https://github.com/apache/trafficcontrol/issues/7246) *Docs* Fixed 
docs for /jobs response description in APIv4 and APIv5.
diff --git 
a/traffic_router/core/src/main/java/org/apache/traffic_control/traffic_router/core/router/StatTracker.java
 
b/traffic_router/core/src/main/java/org/apache/traffic_control/traffic_router/core/router/StatTracker.java
index 610a007d33..1f2e9b5c2b 100644
--- 
a/traffic_router/core/src/main/java/org/apache/traffic_control/traffic_router/core/router/StatTracker.java
+++ 
b/traffic_router/core/src/main/java/org/apache/traffic_control/traffic_router/core/router/StatTracker.java
@@ -36,10 +36,10 @@ public class StatTracker {
 
        public static class Tallies {
 
-               public int getCzCount() {
+               public long getCzCount() {
                        return czCount;
                }
-               public void setCzCount(final int czCount) {
+               public void setCzCount(final long czCount) {
                        this.czCount = czCount;
                }
                public int getGeoCount() {
@@ -97,7 +97,7 @@ public class StatTracker {
                        this.regionalAlternateCount = regionalAlternateCount;
                }
 
-               public int czCount;
+               public long czCount;
                public int geoCount;
                public int deepCzCount;
                public int missCount;
@@ -373,7 +373,7 @@ public class StatTracker {
                                        totalDnsCount++;
                                        totalDnsTime += t.time;
                                }
-                               map = dnsMap;
+                               map = getDnsMap();
 
                                if (t.resultDetails == 
Track.ResultDetails.LOCALIZED_DNS) {
                                        return;
@@ -387,7 +387,7 @@ public class StatTracker {
                                        totalHttpCount++;
                                        totalHttpTime += t.time;
                                }
-                               map = httpMap;
+                               map = getHttpMap();
                        }
                        map.putIfAbsent(fqdn, new Tallies());
                        incTally(t, map.get(fqdn));
@@ -402,6 +402,9 @@ public class StatTracker {
                        break;
                case CZ:
                        tallies.czCount++;
+                       if (tallies.czCount < 0) {
+                               tallies.czCount = Long.MAX_VALUE;
+                       }
                        break;
                case GEO:
                        tallies.geoCount++;
diff --git 
a/traffic_router/core/src/test/java/org/apache/traffic_control/traffic_router/core/router/StatTrackerTest.java
 
b/traffic_router/core/src/test/java/org/apache/traffic_control/traffic_router/core/router/StatTrackerTest.java
new file mode 100644
index 0000000000..3c8c8b0faf
--- /dev/null
+++ 
b/traffic_router/core/src/test/java/org/apache/traffic_control/traffic_router/core/router/StatTrackerTest.java
@@ -0,0 +1,43 @@
+/*
+ *
+ * Licensed 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.traffic_control.traffic_router.core.router;
+
+import org.junit.Test;
+import java.util.HashMap;
+import java.util.Map;
+import static org.powermock.api.mockito.PowerMockito.spy;
+import static org.powermock.api.mockito.PowerMockito.when;
+import static org.springframework.test.util.AssertionErrors.assertEquals;
+
+public class StatTrackerTest {
+    @Test
+    public void testIncTally() {
+        StatTracker tracker = spy(new StatTracker());
+        StatTracker.Tallies tallies = new StatTracker.Tallies();
+        StatTracker.Track track = StatTracker.getTrack();
+
+        tallies.setCzCount(Long.MAX_VALUE);
+
+        Map<String, StatTracker.Tallies> map = new HashMap<>();
+        map.put("blah", tallies);
+        when(tracker.getDnsMap()).thenReturn(map);
+
+        track.setRouteType(StatTracker.Track.RouteType.DNS, "blah");
+        track.setResult(StatTracker.Track.ResultType.CZ);
+        tracker.saveTrack(track);
+        assertEquals("expected czCount to be max long value but got " + 
tallies.getCzCount(), Long.MAX_VALUE, tallies.getCzCount());
+    }
+}

Reply via email to