Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix integer overflow for czCount #7272

Merged
merged 4 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
srijeet0406 marked this conversation as resolved.
Show resolved Hide resolved
- [#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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -97,7 +97,7 @@ public void setRegionalAlternateCount(final int regionalAlternateCount) {
this.regionalAlternateCount = regionalAlternateCount;
}

public int czCount;
public long czCount;
public int geoCount;
public int deepCzCount;
public int missCount;
Expand Down Expand Up @@ -373,7 +373,7 @@ public void saveTrack(final Track t) {
totalDnsCount++;
totalDnsTime += t.time;
}
map = dnsMap;
map = getDnsMap();

if (t.resultDetails == Track.ResultDetails.LOCALIZED_DNS) {
return;
Expand All @@ -387,7 +387,7 @@ public void saveTrack(final Track t) {
totalHttpCount++;
totalHttpTime += t.time;
}
map = httpMap;
map = getHttpMap();
}
map.putIfAbsent(fqdn, new Tallies());
incTally(t, map.get(fqdn));
Expand All @@ -402,6 +402,9 @@ private static void incTally(final Track t, final Tallies tallies) {
break;
case CZ:
tallies.czCount++;
if (tallies.czCount < 0) {
tallies.czCount = Long.MAX_VALUE;
}
break;
case GEO:
tallies.geoCount++;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 integer value but got " + tallies.getCzCount(), Long.MAX_VALUE, tallies.getCzCount());
srijeet0406 marked this conversation as resolved.
Show resolved Hide resolved
}
}