fix: ByteFmt panic on negative int64 values (#1200)

This commit is contained in:
Copilot authored and GitHub committed 2025-12-28 21:57:40 +08:00
1 parent 3abf6ed933
commit 5515a1c48c
2 files changed
+30 -2

No files matched your search

+14 -1
View File
@@ -5,14 +5,27 @@ import (
"math"
)
const unknownSize = "unknown"
var unitArr = []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
func ByteFmt(size int64) string {
if size == 0 {
return "unknown"
return unknownSize
}
// Handle negative values
if size < 0 {
return unknownSize
}
fs := float64(size)
p := int(math.Log(fs) / math.Log(1024))
// Ensure index is within bounds
if p < 0 {
p = 0
}
if p >= len(unitArr) {
p = len(unitArr) - 1
}
val := fs / math.Pow(1024, float64(p))
_, frac := math.Modf(val)
if frac > 0 {