Skip to content

Commit 5ae7a19

Browse files
committed
overflow check
Signed-off-by: zirain <zirain2009@gmail.com>
1 parent a6d8112 commit 5ae7a19

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

internal/gatewayapi/contexts.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package gatewayapi
77

88
import (
9+
"math"
10+
911
certificatesv1b1 "k8s.io/api/certificates/v1beta1"
1012
corev1 "k8s.io/api/core/v1"
1113
discoveryv1 "k8s.io/api/discovery/v1"
@@ -85,12 +87,18 @@ func (g *GatewayContext) attachEnvoyProxy(resources *resource.Resources, epMap m
8587
}
8688
}
8789

88-
func (g *GatewayContext) IncreaseAttachedListenerSets(count int32) {
89-
if g.Status.AttachedListenerSets == nil && count > 0 {
90-
g.Status.AttachedListenerSets = ptr.To(count)
91-
} else if g.Status.AttachedListenerSets != nil {
92-
*g.Status.AttachedListenerSets += count
90+
func (g *GatewayContext) IncreaseAttachedListenerSets(count uint32) {
91+
countInt32 := int32(count)
92+
if g.Status.AttachedListenerSets == nil {
93+
if countInt32 > 0 {
94+
g.Status.AttachedListenerSets = ptr.To(countInt32)
95+
}
96+
return
9397
}
98+
99+
// Check for potential overflow before adding
100+
newValue := int64(*g.Status.AttachedListenerSets) + int64(countInt32)
101+
*g.Status.AttachedListenerSets = int32(min(newValue, math.MaxInt32))
94102
}
95103

96104
// ListenerContext wraps a Listener and provides helper methods for

internal/gatewayapi/listener.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func (t *Translator) ProcessListeners(gateways []*GatewayContext, xdsIR resource
193193
}
194194

195195
}
196-
gateway.IncreaseAttachedListenerSets(int32(len(gatewayAttachedListenerSets)))
196+
gateway.IncreaseAttachedListenerSets(uint32(len(gatewayAttachedListenerSets)))
197197
}
198198

199199
t.checkOverlappingTLSConfig(gateways)

0 commit comments

Comments
 (0)