Skip to content

Commit 5ebf73c

Browse files
committed
Update codes for lints/security
1 parent 9ed8992 commit 5ebf73c

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

maybe.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,14 +1112,19 @@ func (maybeSelf someDef[T]) ToUintptr() (uintptr, error) {
11121112
if maybeSelf.IsNil() {
11131113
return 0, ErrConversionNil
11141114
}
1115+
// Uintptr size might differ between different CPUs
1116+
maxUintptr := uint64(^uintptr(0))
11151117

11161118
var ref interface{} = maybeSelf.ref
11171119
switch (ref).(type) {
11181120
default:
11191121
return uintptr(0), ErrConversionUnsupported
11201122
case string:
11211123
parseInt, err := strconv.ParseInt((ref).(string), 10, 64)
1122-
return uintptr(parseInt), err
1124+
if uint64(parseInt) < maxUintptr {
1125+
return uintptr(parseInt), err
1126+
}
1127+
return uintptr(0), ErrConversionSizeOverflow
11231128
case bool:
11241129
val, err := maybeSelf.ToBool()
11251130
if val {
@@ -1137,7 +1142,10 @@ func (maybeSelf someDef[T]) ToUintptr() (uintptr, error) {
11371142
return uintptr(val), err
11381143
case uint64:
11391144
val, err := maybeSelf.ToUint64()
1140-
return uintptr(val), err
1145+
if val < maxUintptr {
1146+
return uintptr(val), err
1147+
}
1148+
return uintptr(0), ErrConversionSizeOverflow
11411149
case uintptr:
11421150
return ref.(uintptr), nil
11431151
case byte:
@@ -1157,7 +1165,10 @@ func (maybeSelf someDef[T]) ToUintptr() (uintptr, error) {
11571165
return uintptr(val), err
11581166
case int64:
11591167
val, err := maybeSelf.ToInt64()
1160-
return uintptr(val), err
1168+
if uint64(val) < maxUintptr {
1169+
return uintptr(val), err
1170+
}
1171+
return uintptr(0), ErrConversionSizeOverflow
11611172
case float32:
11621173
val, err := maybeSelf.ToFloat32()
11631174
return uintptr(math.Round(float64(val))), err

0 commit comments

Comments
 (0)