Skip to content

Commit

Permalink
handle potential integer overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
druppelt committed Sep 11, 2024
1 parent e93c51f commit 5a3b3ba
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions internal/calc/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package calc

import (
"fmt"
"math"

appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/intstr"
"math"
)

// calculates the cpu/memory resources a single deployment needs. Replicas and the deployment
Expand Down Expand Up @@ -43,7 +42,7 @@ func deployment(deployment appsv1.Deployment) (*ResourceUsage, error) {
resourceOverhead = 1
podOverhead = 0
case "":
// RollingUpdate is the default an can be an empty string. If so, set the defaults
// RollingUpdate is the default and can be an empty string. If so, set the defaults
// (https://pkg.go.dev/k8s.io/api/apps/v1?tab=doc#RollingUpdateDeployment) and continue calculation.
defaults := intstr.FromString("25%")
strategy = appsv1.DeploymentStrategy{
Expand Down Expand Up @@ -85,7 +84,11 @@ func deployment(deployment appsv1.Deployment) (*ResourceUsage, error) {
}

// podOverhead is the number of pods which can run more during a deployment
podOverhead = int32(maxSurge - maxUnavailable)
podOverheadInt := maxSurge - maxUnavailable
if podOverheadInt > math.MaxInt32 || podOverheadInt < math.MinInt32 {
return nil, fmt.Errorf("deployment: %s maxSurge - maxUnavailable (%d-%d) was out of bounds for int32", deployment.Name, maxSurge, maxUnavailable)
}
podOverhead = int32(podOverheadInt)

Check failure on line 91 in internal/calc/deployment.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion int -> int32 (gosec)

resourceOverhead = (float64(podOverhead) / float64(*replicas)) + 1
default:
Expand Down

0 comments on commit 5a3b3ba

Please sign in to comment.