go-redis/redis · error

redis: failed to parse float %q: %v

Error message

redis: failed to parse float %q: %v

What it means

MustParseFloat panics when ParseStringToFloat fails to convert a string to float64. ParseStringToFloat handles inf/-inf/nan specially then falls back to strconv.ParseFloat, which fails on malformed numeric strings. MustParseFloat is used internally for RESP3 float replies and re-exported via the helper package; the panic indicates an unexpected (corrupt) float from Redis or a bad caller-provided string.

Source

Thrown at internal/util/convert.go:27

// ParseFloat parses a Redis RESP3 float reply into a Go float64,
// handling "inf", "-inf", "nan" per Redis conventions.
func ParseStringToFloat(s string) (float64, error) {
	switch s {
	case "inf":
		return math.Inf(1), nil
	case "-inf":
		return math.Inf(-1), nil
	case "nan", "-nan":
		return math.NaN(), nil
	}
	return strconv.ParseFloat(s, 64)
}

// MustParseFloat is like ParseFloat but panics on parse errors.
func MustParseFloat(s string) float64 {
	f, err := ParseStringToFloat(s)
	if err != nil {
		panic(fmt.Sprintf("redis: failed to parse float %q: %v", s, err))
	}
	return f
}

// SafeIntToInt32 safely converts an int to int32, returning an error if overflow would occur.
func SafeIntToInt32(value int, fieldName string) (int32, error) {
	if value > math.MaxInt32 {
		return 0, fmt.Errorf("redis: %s value %d exceeds maximum allowed value %d", fieldName, value, math.MaxInt32)
	}
	if value < math.MinInt32 {
		return 0, fmt.Errorf("redis: %s value %d is below minimum allowed value %d", fieldName, value, math.MinInt32)
	}
	return int32(value), nil
}

View on GitHub (pinned to 36d97525cd)

Solutions

  1. If calling the public helper, use util.ParseStringToFloat (which returns an error) instead of MustParseFloat and handle the error.
  2. Validate the string is numeric before calling MustParseFloat when working with untrusted input.
  3. If this surfaces from internal RESP3 parsing, inspect the server/proxy output and connection integrity (TLS, network errors).

Example fix

// before (helper)
f := util.MustParseFloat(userInput) // panics on "abc"

// after
f, err := util.ParseStringToFloat(userInput)
if err != nil { return err }
Defensive patterns

Strategy: validation

Validate before calling

f, err := util.ParseStringToFloat(s)
if err != nil {
    return 0, fmt.Errorf("invalid float %q: %w", s, err)
}

Try / catch

// If you must keep MustParseFloat on untrusted input, isolate the panic:
func safeFloat(s string) (f float64, err error) {
    defer func() {
        if r := recover(); r != nil {
            err = fmt.Errorf("parse float %q: %v", s, r)
        }
    }()
    return util.MustParseFloat(s), nil
}

Prevention

When it happens

Trigger: Internally when parsing a RESP3 float reply that is not a valid number (server/protocol corruption). Externally when a caller passes a non-numeric string to helper.MustParseFloat / util.MustParseFloat.

Common situations: A custom RESP proxy emitting malformed floats, RESP2/RESP3 protocol corruption on the wire, or a caller misusing the public MustParseFloat helper on untrusted input.

Related errors


AI-assisted analysis of go-redis/redis@36d97525cd (2026-08-06). Data as JSON: /data/errors/ad9846c195115f46.json. Report an issue: GitHub.