diff --git a/pkgs/mcpx/internal/settings/registry_test.go b/pkgs/mcpx/internal/settings/registry_test.go index 53f2d951d..c02e47ffb 100644 --- a/pkgs/mcpx/internal/settings/registry_test.go +++ b/pkgs/mcpx/internal/settings/registry_test.go @@ -245,6 +245,32 @@ func TestByteSizesAcceptBothDecimalAndBinarySuffixes(t *testing.T) { } } +// #1697: negative and int64-overflowing sizes are refused, with the +// offending value named, so no layer (file/env/flag/preset/runtime) can +// admit one and crash a consumer spending it as a slice capacity. +func TestParseBytesRefusesNegativeAndOverflowingSizes(t *testing.T) { + for _, in := range []string{"-1", "-5MB", "-1KiB", "999999999999999999999999TB", "9223372036854775808"} { + got, err := settings.ParseBytes(in) + if err == nil { + t.Errorf("ParseBytes(%q) = %d, nil; want an error", in, got) + } else if !strings.Contains(err.Error(), in) { + t.Errorf("ParseBytes(%q) error %q does not name the offending value", in, err) + } + } + if got, err := settings.ParseBytes("0"); err != nil || got != 0 { + t.Errorf("ParseBytes(\"0\") = %d, %v; want 0, nil (zero keeps its documented meaning)", got, err) + } + set := settings.Setting{Path: "http.streamBufferInit", Kind: settings.KindBytes} + for _, in := range []string{"-1", "-5MB", "999999999999999999999999TB"} { + if err := settings.Validate(set, in); err == nil { + t.Errorf("Validate(KindBytes, %q) = nil; want an error", in) + } + } + if err := settings.Validate(set, "16MB"); err != nil { + t.Errorf("Validate(KindBytes, \"16MB\") = %v; want nil", err) + } +} + func TestRequirementsCatchASettingThatWouldBeSilentlyInert(t *testing.T) { s, err := settings.New([]settings.Setting{ {Path: "a.on", Kind: settings.KindBool, Default: "false", Name: "A", Short: "a"}, diff --git a/pkgs/mcpx/internal/settings/value.go b/pkgs/mcpx/internal/settings/value.go index ce3820a70..5104590c6 100644 --- a/pkgs/mcpx/internal/settings/value.go +++ b/pkgs/mcpx/internal/settings/value.go @@ -446,12 +446,16 @@ func dedupe(in []string) []string { // ParseBytes reads 16MB, 1GiB, 1024. Decimal and binary suffixes both work // because both appear in the wild and arguing about which is correct helps -// nobody configure a log file. +// nobody configure a log file. Negative sizes and magnitudes that do not +// fit in an int64 are refused (#1697): a size is spent as a byte count or a +// slice capacity downstream, where a negative is a panic and an overflowed +// one silently becomes the most negative int64 there is. func ParseBytes(v string) (int64, error) { v = strings.TrimSpace(v) if v == "" { return 0, nil } + orig := v mult := int64(1) upper := strings.ToUpper(v) for _, suf := range []struct { @@ -474,7 +478,15 @@ func ParseBytes(v string) (int64, error) { if err != nil { return 0, fmt.Errorf("not a size: %q", v) } - return int64(f * float64(mult)), nil + scaled := f * float64(mult) + if scaled >= 9223372036854775808.0 { // 2^63: the first magnitude int64 cannot hold + return 0, fmt.Errorf("size too large: %q", orig) + } + n := int64(scaled) + if n < 0 { + return 0, fmt.Errorf("size must not be negative: %q", orig) + } + return n, nil } // Validate checks one raw value against its declaration.