fs: reject NaN sizes and CountSuffix values that overflow

SizeSuffix.Set and CountSuffix.Set accepted "nan" before a suffix, so
--max-transfer nank became -1 ("off") without an error. CountSuffix.Set
also had no overflow check, unlike SizeSuffix, so 10E or infk wrapped
to the same value.
This commit is contained in:
kevin9327 authored and Nick Craig-Wood committed 2026-10-31 12:27:06 +00:00
1 parent 0de32e2269
commit 18c7de0920
4 files changed
+17

No files matched your search

+6
View File
@@ -139,10 +139,16 @@ func (x *CountSuffix) Set(s string) error {
if err != nil {
return err
}
if math.IsNaN(value) {
return fmt.Errorf("size must be a number %q", s)
}
if value < 0 {
return fmt.Errorf("size can't be negative %q", s)
}
value *= multiplier
if value >= float64(CountSuffixMaxValue) {
return fmt.Errorf("value can't be greater than %d", int64(CountSuffixMaxValue))
}
*x = CountSuffix(value)
return nil
}
+6
View File
@@ -95,6 +95,12 @@ func TestCountSuffixSet(t *testing.T) {
{"1iB", 0, true},
{"1Ki", 0, true},
{"1KiB", 0, true},
{"9E", 9 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000, false},
{"9.3E", 0, true},
{"10E", 0, true},
{"infk", 0, true},
{"nank", 0, true},
{"NaNM", 0, true},
} {
ss := CountSuffix(0)
err := ss.Set(test.in)
+3
View File
@@ -190,6 +190,9 @@ func (x *SizeSuffix) Set(s string) error {
if err != nil {
return err
}
if math.IsNaN(value) {
return fmt.Errorf("size must be a number %q", s)
}
if value < 0 {
return fmt.Errorf("size can't be negative %q", s)
}
+2
View File
@@ -131,6 +131,8 @@ func TestSizeSuffixSet(t *testing.T) {
{"1i", 0, true},
{"1iB", 0, true},
{"1MB", 0, true},
{"nank", 0, true},
{"NaNM", 0, true},
} {
ss := SizeSuffix(0)
err := ss.Set(test.in)