Skip to content

Commit

Permalink
fix: Code Scanning #67-#69, integer overflow conversion uint64 -> int64
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed Dec 1, 2024
1 parent 864af30 commit 89aa21e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
17 changes: 8 additions & 9 deletions pkg/system/filesize.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import (
"golang.org/x/sys/unix"
)

func DiskAvailable() (int64, error) {
func DiskAvailable() (uint64, error) {
var stat unix.Statfs_t
wd, err := os.Getwd()

if err != nil {
return 0, err
}
Expand All @@ -22,11 +21,11 @@ func DiskAvailable() (int64, error) {
return 0, err
}

return int64(stat.Bavail * uint64(stat.Bsize)), nil
return uint64(stat.Bavail) * uint64(stat.Bsize), nil
}

// https://stackoverflow.com/questions/32482673/how-to-get-directory-total-size
func DirSize(path string) (int64, error) {
// uint64 ver https://stackoverflow.com/questions/32482673/how-to-get-directory-total-size
func DirSize(path string) (uint64, error) {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
Expand All @@ -37,7 +36,7 @@ func DirSize(path string) (int64, error) {
}
return err
})
return size, err
return uint64(size), err
}

// https://hakk.dev/docs/golang-convert-file-size-human-readable/
Expand All @@ -54,13 +53,13 @@ func fileSizeRound(val float64, roundOn float64, places int) float64 {
return round / pow
}

// https://programming.guide/go/formatting-byte-size-to-human-readable-format.html
func ByteCountDecimal(b int64) string {
// uint64 ver https://programming.guide/go/formatting-byte-size-to-human-readable-format.html
func ByteCountDecimal(b uint64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
div, exp := uint64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
Expand Down
4 changes: 2 additions & 2 deletions pkg/system/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"strings"
)

func GetMemoryUsageAndGoroutine() (int64, string) {
func GetMemoryUsageAndGoroutine() (uint64, string) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
return int64(m.Alloc), strconv.Itoa(runtime.NumGoroutine())
return m.Alloc, strconv.Itoa(runtime.NumGoroutine())
}

func Stats(logPrint bool) string {
Expand Down

0 comments on commit 89aa21e

Please sign in to comment.