Skip to content

Commit fa73682

Browse files
committed
G115 go sec fix
1 parent faea4af commit fa73682

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package common
2+
3+
// SafeUint32 converts int to uint32 with bounds checking to prevent overflow
4+
func SafeUint32(value int) uint32 {
5+
if value < 0 {
6+
return 0
7+
}
8+
// On 32-bit systems, int max is smaller than uint32 max, so no overflow possible
9+
// On 64-bit systems, we need to check against uint32 max
10+
if ^uint(0)>>32 == 0 {
11+
// 32-bit system: int and uint32 have same size, no overflow possible
12+
return uint32(value)
13+
}
14+
// 64-bit system: check against uint32 max
15+
if uint64(value) > 0xFFFFFFFF {
16+
return 0xFFFFFFFF
17+
}
18+
return uint32(value)
19+
}

spectrocloud/kubevirt/schema/virtualmachineinstance/domain_spec.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"k8s.io/apimachinery/pkg/api/resource"
1010
kubevirtapiv1 "kubevirt.io/api/core/v1"
1111

12+
"github.com/spectrocloud/terraform-provider-spectrocloud/spectrocloud/common"
1213
"github.com/spectrocloud/terraform-provider-spectrocloud/spectrocloud/kubevirt/utils"
1314
)
1415

@@ -295,7 +296,7 @@ func expandCPU(cpu map[string]interface{}) (kubevirtapiv1.CPU, error) {
295296
if v > math.MaxInt { // Cap to max representable int on this architecture
296297
return result, fmt.Errorf("cores value %d is out of range for uint32", v)
297298
}
298-
result.Cores = uint32(v)
299+
result.Cores = common.SafeUint32(v)
299300
}
300301
if v, ok := cpu["sockets"].(int); ok {
301302
if v < 0 {
@@ -304,7 +305,7 @@ func expandCPU(cpu map[string]interface{}) (kubevirtapiv1.CPU, error) {
304305
if v > math.MaxInt { // Cap to max representable int on this architecture
305306
return result, fmt.Errorf("sockets value %d is out of range for uint32", v)
306307
}
307-
result.Sockets = uint32(v)
308+
result.Sockets = common.SafeUint32(v)
308309
}
309310
if v, ok := cpu["threads"].(int); ok {
310311
if v < 0 {
@@ -313,7 +314,7 @@ func expandCPU(cpu map[string]interface{}) (kubevirtapiv1.CPU, error) {
313314
if v > math.MaxInt { // Cap to max representable int on this architecture
314315
return result, fmt.Errorf("threads value %d is out of range for uint32", v)
315316
}
316-
result.Threads = uint32(v)
317+
result.Threads = common.SafeUint32(v)
317318
}
318319

319320
return result, nil

spectrocloud/utils.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package spectrocloud
33
import (
44
"math"
55
"time"
6+
7+
"github.com/spectrocloud/terraform-provider-spectrocloud/spectrocloud/common"
68
)
79

810
// SafeInt32 converts int to int32 with bounds checking to prevent overflow
@@ -24,13 +26,7 @@ func SafeInt64(value int) int64 {
2426

2527
// SafeUint32 converts int to uint32 with bounds checking to prevent overflow
2628
func SafeUint32(value int) uint32 {
27-
if value < 0 {
28-
return 0
29-
}
30-
if value > math.MaxInt {
31-
return math.MaxUint32
32-
}
33-
return uint32(value)
29+
return common.SafeUint32(value)
3430
}
3531

3632
func expandStringList(configured []interface{}) []string {

0 commit comments

Comments
 (0)