Skip to content

Commit

Permalink
Address sast: integer overflow conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
hebelsan committed Nov 7, 2024
1 parent be3a992 commit 107ce86
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/controller/infrastructure/infraflow/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,7 @@ func cidrSubnet(baseCIDR string, newPrefixLength int, index int) (string, error)
return "", fmt.Errorf("invalid new prefix length")
}

// #nosec: G115
offset := big.NewInt(0).Mul(big.NewInt(int64(index)), big.NewInt(0).Lsh(big.NewInt(1), uint(addrSize-newPrefixLength)))
subnetIP := net.IP(big.NewInt(0).Add(big.NewInt(0).SetBytes(baseIP), offset).Bytes())
return fmt.Sprintf("%s/%d", subnetIP.String(), newPrefixLength), nil
Expand Down
9 changes: 6 additions & 3 deletions pkg/controller/worker/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package worker
import (
"context"
"fmt"
"math"
"path/filepath"
"slices"
"sort"
Expand Down Expand Up @@ -107,8 +108,6 @@ func (w *workerDelegate) generateMachineConfig(ctx context.Context) error {
}

for _, pool := range w.worker.Spec.Pools {
zoneLen := int32(len(pool.Zones))

workerConfig := &awsapi.WorkerConfig{}
if pool.ProviderConfig != nil && pool.ProviderConfig.Raw != nil {
if _, _, err := w.decoder.Decode(pool.ProviderConfig.Raw, nil, workerConfig); err != nil {
Expand Down Expand Up @@ -154,8 +153,12 @@ func (w *workerDelegate) generateMachineConfig(ctx context.Context) error {
return err
}

if len(pool.Zones) > math.MaxInt32 {
return fmt.Errorf("too many zones")
}
zoneLen := int32(len(pool.Zones)) // #nosec: G115 - We do check if pool Zones exceeds max_int32.
for zoneIndex, zone := range pool.Zones {
zoneIdx := int32(zoneIndex)
zoneIdx := int32(zoneIndex) // #nosec: G115 - We do check if pool Zones exceeds max_int32.

nodesSubnet, err := awsapihelper.FindSubnetForPurposeAndZone(infrastructureStatus.VPC.Subnets, awsapi.PurposeNodes, zone)
if err != nil {
Expand Down

0 comments on commit 107ce86

Please sign in to comment.