Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pkgs/mcpx/internal/settings/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
9 changes: 8 additions & 1 deletion pkgs/mcpx/internal/settings/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package settings
import (
"encoding/json"
"fmt"
"math"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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)
}
Expand Down
Loading