Skip to content

Commit

Permalink
Update codes for lints/security
Browse files Browse the repository at this point in the history
  • Loading branch information
johnteee committed Jun 2, 2022
1 parent 9ed8992 commit 5ebf73c
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions maybe.go
Original file line number Diff line number Diff line change
Expand Up @@ -1112,14 +1112,19 @@ func (maybeSelf someDef[T]) ToUintptr() (uintptr, error) {
if maybeSelf.IsNil() {
return 0, ErrConversionNil
}
// Uintptr size might differ between different CPUs
maxUintptr := uint64(^uintptr(0))

var ref interface{} = maybeSelf.ref
switch (ref).(type) {
default:
return uintptr(0), ErrConversionUnsupported
case string:
parseInt, err := strconv.ParseInt((ref).(string), 10, 64)
return uintptr(parseInt), err
if uint64(parseInt) < maxUintptr {
return uintptr(parseInt), err
}
return uintptr(0), ErrConversionSizeOverflow
case bool:
val, err := maybeSelf.ToBool()
if val {
Expand All @@ -1137,7 +1142,10 @@ func (maybeSelf someDef[T]) ToUintptr() (uintptr, error) {
return uintptr(val), err
case uint64:
val, err := maybeSelf.ToUint64()
return uintptr(val), err
if val < maxUintptr {
return uintptr(val), err
}
return uintptr(0), ErrConversionSizeOverflow
case uintptr:
return ref.(uintptr), nil
case byte:
Expand All @@ -1157,7 +1165,10 @@ func (maybeSelf someDef[T]) ToUintptr() (uintptr, error) {
return uintptr(val), err
case int64:
val, err := maybeSelf.ToInt64()
return uintptr(val), err
if uint64(val) < maxUintptr {
return uintptr(val), err
}
return uintptr(0), ErrConversionSizeOverflow
case float32:
val, err := maybeSelf.ToFloat32()
return uintptr(math.Round(float64(val))), err
Expand Down

0 comments on commit 5ebf73c

Please sign in to comment.