nats-io/nats-server · error

negative number (%d) not allowed: %s

Error message

negative number (%d) not allowed: %s

What it means

Cron expression integer parser: strconv.Atoi succeeded but the value is negative, which no cron field accepts; the offending subexpression is included.

Source

Thrown at server/cron.go:253

// parseIntOrName returns the (possibly-named) integer contained in expr.
func parseIntOrName(expr string, names map[string]uint) (uint, error) {
	if names != nil {
		if namedInt, ok := names[strings.ToLower(expr)]; ok {
			return namedInt, nil
		}
	}
	return mustParseInt(expr)
}

// mustParseInt parses the given expression as an int or returns an error.
func mustParseInt(expr string) (uint, error) {
	num, err := strconv.Atoi(expr)
	if err != nil {
		return 0, fmt.Errorf("failed to parse int from %s: %s", expr, err)
	}
	if num < 0 {
		return 0, fmt.Errorf("negative number (%d) not allowed: %s", num, expr)
	}
	return uint(num), nil
}

// getBits sets all bits in the range [min, max], modulo the given step size.
func getBits(min, max, step uint) uint64 {
	var bits uint64

	// If step is 1, use shifts.
	if step == 1 {
		return ^(math.MaxUint64 << (max + 1)) & (math.MaxUint64 << min)
	}

	// Else, use a simple loop.
	for i := min; i <= max; i += step {
		bits |= 1 << i
	}
	return bits

View on GitHub (pinned to 3a66a489d2)

Solutions

  1. Remove the minus sign and use the intended non-negative value
  2. Fix doubled hyphens in ranges ("1-3", not "1--3")
  3. Interpolate template variables before scheduling and validate the rendered expression

Example fix

// before (minute field)
 expr := "1--3"
// after
 expr := "1-3"
Defensive patterns

Strategy: validation

Validate before calling

func isNonNegativeInt(s string) bool {
    n, err := strconv.Atoi(s)
    return err == nil && n >= 0
}

Prevention

When it happens

Trigger: A cron field token starting with '-', e.g. "-5", "1--3" (second token '-3'), or "*-5", reaching mustParseInt via getRange or parseIntOrName in parseCron.

Common situations: Accidentally pasting a leading minus; malformed ranges with doubled hyphens; template variables that rendered empty leaving a stray '-' prefix.

Related errors


AI-assisted analysis of nats-io/nats-server@3a66a489d2 (2026-09-02). Data as JSON: /api/errors/4a1e3a87071d3f34. Report an issue: GitHub.