Use explicit integer bounds in Proxmox parsing

This commit is contained in:
rcourtman 2026-03-31 09:43:04 +01:00
parent 6a5a5ee615
commit 9155480bbd
3 changed files with 9 additions and 12 deletions

View file

@ -1606,7 +1606,9 @@ func (a *VMIpAddress) UnmarshalJSON(data []byte) error {
if prefix > 128 {
prefix = 128
}
a.Prefix = int(prefix)
if prefix <= 128 {
a.Prefix = int(prefix)
}
return nil
}

View file

@ -2,20 +2,15 @@ package proxmox
import "math"
const (
maxInt = int(^uint(0) >> 1)
minInt = -maxInt - 1
)
func intFromInt64Checked(v int64) (int, bool) {
if v > int64(maxInt) || v < int64(minInt) {
if v > int64(math.MaxInt) || v < int64(math.MinInt) {
return 0, false
}
return int(v), true
}
func intFromUint64Checked(v uint64) (int, bool) {
if v > uint64(maxInt) {
if v > uint64(math.MaxInt) {
return 0, false
}
return int(v), true
@ -26,7 +21,7 @@ func intFromFloat64RoundedChecked(v float64) (int, bool) {
return 0, false
}
rounded := math.Round(v)
if rounded > float64(maxInt) || rounded < float64(minInt) {
if rounded > float64(math.MaxInt) || rounded < float64(math.MinInt) {
return 0, false
}
return int(rounded), true
@ -37,7 +32,7 @@ func intFromFloat64TruncChecked(v float64) (int, bool) {
return 0, false
}
truncated := math.Trunc(v)
if truncated > float64(maxInt) || truncated < float64(minInt) {
if truncated > float64(math.MaxInt) || truncated < float64(math.MinInt) {
return 0, false
}
return int(truncated), true

View file

@ -123,8 +123,8 @@ func TestIntFromAny(t *testing.T) {
{"string empty", "", 0, false},
{"string whitespace", " 42 ", 42, true},
{"string invalid", "abc", 0, false},
{"uint64 overflow", uint64(maxInt) + 1, 0, false},
{"float64 overflow", float64(maxInt) * 2, 0, false},
{"uint64 overflow", uint64(math.MaxInt) + 1, 0, false},
{"float64 overflow", float64(math.MaxInt) * 2, 0, false},
}
for _, tc := range tests {