junegunn/fzf · error

${label} must be non-negative

Error message

${label} must be non-negative

What it means

Thrown by parseSize when a percentage value is negative. Several size-like options (height, min-height as percentages, preview window size, margins, padding) accept either an absolute integer or a 'NN%' form; the percent branch parses the number with atof and rejects values below zero before even checking the per-option maximum.

Source

Thrown at src/options.go:2236

	return nil
}

func strLines(str string) []string {
	return strings.Split(strings.TrimSuffix(str, "\n"), "\n")
}

func parseSize(str string, maxPercent float64, label string) (sizeSpec, error) {
	var spec = sizeSpec{}
	var val float64
	var err error
	percent := strings.HasSuffix(str, "%")
	if percent {
		if val, err = atof(str[:len(str)-1]); err != nil {
			return spec, err
		}

		if val < 0 {
			return spec, errors.New(label + " must be non-negative")
		}
		if val > maxPercent {
			return spec, fmt.Errorf("%s too large (max: %d%%)", label, int(maxPercent))
		}
	} else {
		if strings.Contains(str, ".") {
			return spec, errors.New(label + " (without %) must be a non-negative integer")
		}

		i, err := atoi(str)
		if err != nil {
			return spec, err
		}
		val = float64(i)
		if val < 0 {
			return spec, errors.New(label + " must be non-negative")
		}
	}

View on GitHub (pinned to bd4efa277b)

Solutions

  1. Clamp computed percentages to >= 0 before passing them (e.g. use a max(0, x) helper in shell)
  2. Quote the argument so '-20%' is not swallowed as a flag: `--height='-20%'` still errors but '--height' '-20' style mistakes become visible
  3. Use absolute sizes instead of percentages when the source value can legitimately be small

Example fix

# before
fzf --height "$((${LINES}-60))%"
# after
H=$((LINES-60)); [ "$H" -lt 0 ] && H=0
fzf --height "${H}%"
Defensive patterns

Strategy: validation

Validate before calling

# bash: percent sizes must be >= 0
pct_ok() { [[ "$1" =~ ^-?[0-9]+%$ ]] || return 0; [[ "$1" =~ ^- ]] && return 1 || return 0; }
pct_ok "$HEIGHT" || { echo "negative percent" >&2; exit 1; }

Prevention

When it happens

Trigger: `--height '-50%'`, `--preview-window '-20%'`, `--margin '-1%,...'`, `--min-height '-5%'` — any percent spec where the numeric part is negative.

Common situations: Scripts computing a height percentage from terminal lines that can go negative on tiny terminals; shell arithmetic like "$((${LINES}/2-40))%" producing a minus sign; unquoted minus being interpreted as an option flag.

Related errors


AI-assisted analysis of junegunn/fzf@bd4efa277b (2026-08-15). Data as JSON: /api/errors/a1a4354f9ef8d432. Report an issue: GitHub.