From 8643e8a5bc6ff722ed518eae67961baf23aace4e Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Tue, 3 Nov 2026 01:47:47 -0600 Subject: [PATCH] fix(mcpx): refuse non-finite ParseBytes spellings explicitly (#3521) ParseBytes refused NaN/Inf only incidentally (float-to-int conversion for NaN, magnitude check for infinities). Add an explicit math.IsNaN/IsInf guard naming the offending value, and name the non-finite spellings (bare and suffixed) in the refusal test table and the Validate loop so a future float-side refactor regresses loudly. --- pkgs/mcpx/internal/settings/registry_test.go | 8 ++++++-- pkgs/mcpx/internal/settings/value.go | 9 ++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pkgs/mcpx/internal/settings/registry_test.go b/pkgs/mcpx/internal/settings/registry_test.go index c02e47ffbe..2df57d70d8 100644 --- a/pkgs/mcpx/internal/settings/registry_test.go +++ b/pkgs/mcpx/internal/settings/registry_test.go @@ -248,8 +248,11 @@ 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. +// #3521: non-finite spellings are named too, so a future refactor to +// float-side comparisons (which NaN defeats) regresses loudly. func TestParseBytesRefusesNegativeAndOverflowingSizes(t *testing.T) { - for _, in := range []string{"-1", "-5MB", "-1KiB", "999999999999999999999999TB", "9223372036854775808"} { + for _, in := range []string{"-1", "-5MB", "-1KiB", "999999999999999999999999TB", "9223372036854775808", + "NaN", "nan", "Inf", "+Inf", "-Inf", "Infinity", "NaNMB", "InfGB"} { got, err := settings.ParseBytes(in) if err == nil { t.Errorf("ParseBytes(%q) = %d, nil; want an error", in, got) @@ -261,7 +264,8 @@ func TestParseBytesRefusesNegativeAndOverflowingSizes(t *testing.T) { 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"} { + for _, in := range []string{"-1", "-5MB", "999999999999999999999999TB", + "NaN", "nan", "Inf", "+Inf", "-Inf", "Infinity", "NaNMB", "InfGB"} { if err := settings.Validate(set, in); err == nil { t.Errorf("Validate(KindBytes, %q) = nil; want an error", in) } diff --git a/pkgs/mcpx/internal/settings/value.go b/pkgs/mcpx/internal/settings/value.go index 7ac81d1a0e..a3598f3b58 100644 --- a/pkgs/mcpx/internal/settings/value.go +++ b/pkgs/mcpx/internal/settings/value.go @@ -3,6 +3,7 @@ package settings import ( "encoding/json" "fmt" + "math" "sort" "strconv" "strings" @@ -449,7 +450,10 @@ func dedupe(in []string) []string { // 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. +// one silently becomes the most negative int64 there is. Non-finite spellings +// (NaN, Inf, bare or suffixed) are refused explicitly (#3521): without the +// guard they are caught only incidentally, by float-to-int conversion for +// NaN and by the magnitude check for infinities. func ParseBytes(v string) (int64, error) { v = strings.TrimSpace(v) if v == "" { @@ -479,6 +483,9 @@ func ParseBytes(v string) (int64, error) { return 0, fmt.Errorf("not a size: %q", v) } scaled := f * float64(mult) + if math.IsNaN(scaled) || math.IsInf(scaled, 0) { + return 0, fmt.Errorf("size must be finite: %q", orig) + } if scaled >= 9223372036854775808.0 { // 2^63: the first magnitude int64 cannot hold return 0, fmt.Errorf("size too large: %q", orig) }