Skip to content

Commit

Permalink
fix(lint): update incorrect conversion of int to use math min and mat…
Browse files Browse the repository at this point in the history
…h int constants 🐛
  • Loading branch information
qnen committed Dec 12, 2024
1 parent e0d8962 commit 02905b5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pkg/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
"fmt"
"math"
"os"
"reflect"
"strconv"
Expand Down Expand Up @@ -49,7 +50,7 @@ func GetenvIntOrDefault(key string, defaultValue int64) int64 {
return defaultValue
}

if val > int64(^uint(0)>>1) || val < int64(^uint(0)>>1+1) {
if val > math.MaxInt64 || val < math.MinInt64 {
return defaultValue
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"math"
"os/exec"
"reflect"
"regexp"
Expand Down Expand Up @@ -147,10 +148,10 @@ func SafeIntToUint64(val int) uint64 {

// SafeInt64ToInt safely converts int64 to int
func SafeInt64ToInt(val int64) int {
if val > int64(int(^uint(0)>>1)) {
return int(^uint(0) >> 1)
} else if val < int64(-int(^uint(0)>>1)-1) {
return -int(^uint(0)>>1) - 1
if val > math.MaxInt64 {
return math.MaxInt64
} else if val < math.MinInt64 {
return math.MinInt64
}

return int(val)

Check failure

Code scanning / CodeQL

Incorrect conversion between integer types High

Incorrect conversion of a signed 64-bit integer from
strconv.ParseInt
to a lower bit size type int without an upper bound check.
Expand Down

0 comments on commit 02905b5

Please sign in to comment.