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

Prevent Int64 Overflow #3605

Merged
merged 9 commits into from
Jan 24, 2024
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
17 changes: 13 additions & 4 deletions pkg/fleets/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"encoding/json"
"fmt"
"math"
"time"

"agones.dev/agones/pkg/apis/agones"
Expand Down Expand Up @@ -721,6 +722,14 @@ func (c *Controller) filterGameServerSetByActive(fleet *agonesv1.Fleet, list []*
return active, rest
}

// SafeAdd prevents overflow by limiting the sum to math.MaxInt64.
func SafeAdd(x, y int64) int64 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should probably go in pkg/apis/agones/v1/common.go

if x > math.MaxInt64-y {
return math.MaxInt64
}
return x + y
}

// mergeCounters adds the contents of AggregatedCounterStatus c2 into c1.
func mergeCounters(c1, c2 map[string]agonesv1.AggregatedCounterStatus) map[string]agonesv1.AggregatedCounterStatus {
if c1 == nil {
Expand All @@ -730,10 +739,10 @@ func mergeCounters(c1, c2 map[string]agonesv1.AggregatedCounterStatus) map[strin
for key, val := range c2 {
// If the Counter exists in both maps, aggregate the values.
if counter, ok := c1[key]; ok {
counter.AllocatedCapacity += val.AllocatedCapacity
counter.AllocatedCount += val.AllocatedCount
counter.Capacity += val.Capacity
counter.Count += val.Count
counter.AllocatedCapacity = SafeAdd(counter.AllocatedCapacity, val.AllocatedCapacity)
counter.AllocatedCount = SafeAdd(counter.AllocatedCount, val.AllocatedCount)
counter.Capacity = SafeAdd(counter.Capacity, val.Capacity)
counter.Count = SafeAdd(counter.Count, val.Count)
c1[key] = counter
} else {
c1[key] = *val.DeepCopy()
Expand Down
10 changes: 6 additions & 4 deletions pkg/gameserversets/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
getterv1 "agones.dev/agones/pkg/client/clientset/versioned/typed/agones/v1"
"agones.dev/agones/pkg/client/informers/externalversions"
listerv1 "agones.dev/agones/pkg/client/listers/agones/v1"
"agones.dev/agones/pkg/fleets"
"agones.dev/agones/pkg/gameservers"
"agones.dev/agones/pkg/util/crd"
"agones.dev/agones/pkg/util/logfields"
Expand Down Expand Up @@ -679,12 +680,13 @@ func aggregateCounters(aggCounterStatus map[string]agonesv1.AggregatedCounterSta
// If the Counter exists in both maps, aggregate the values.
if counter, ok := aggCounterStatus[key]; ok {
// Aggregate for all game server statuses (expected IsBeingDeleted)
counter.Count += val.Count
counter.Capacity += val.Capacity
counter.Count = fleets.SafeAdd(counter.Count, val.Count)
counter.Capacity = fleets.SafeAdd(counter.Capacity, val.Capacity)

// Aggregate for Allocated game servers only
if gsState == agonesv1.GameServerStateAllocated {
counter.AllocatedCount += val.Count
counter.AllocatedCapacity += val.Capacity
counter.AllocatedCount = fleets.SafeAdd(counter.AllocatedCount, val.Count)
counter.AllocatedCapacity = fleets.SafeAdd(counter.AllocatedCapacity, val.Capacity)
}
aggCounterStatus[key] = counter
} else {
Expand Down