larksuite/cli · error

invalid negative content range: %d-%d

Error message

invalid negative content range: %d-%d

What it means

The parsed start or end offset of the Content-Range was negative, which is invalid for the 'start-end/total' inclusive form this library requires (it does not implement suffix ranges like 'bytes -100/500').

Source

Thrown at extension/download/download.go:708

		return contentRange{}, fmt.Errorf("unsupported content-range: %q", header)
	}
	start, err := strconv.ParseInt(bounds[0], 10, 64)
	if err != nil {
		return contentRange{}, fmt.Errorf("parse range start: %w", err)
	}
	end, err := strconv.ParseInt(bounds[1], 10, 64)
	if err != nil {
		return contentRange{}, fmt.Errorf("parse range end: %w", err)
	}
	total, err := strconv.ParseInt(parts[1], 10, 64)
	if err != nil {
		return contentRange{}, fmt.Errorf("parse total size: %w", err)
	}
	if total <= 0 {
		return contentRange{}, fmt.Errorf("invalid total size: %d", total)
	}
	if start < 0 || end < 0 {
		return contentRange{}, fmt.Errorf("invalid negative content range: %d-%d", start, end)
	}
	if start > end {
		return contentRange{}, fmt.Errorf("invalid content range: start %d is after end %d", start, end)
	}
	if end >= total {
		return contentRange{}, fmt.Errorf("invalid content range: end %d is outside total %d", end, total)
	}
	return contentRange{start: start, end: end, total: total}, nil
}

// strongETag returns a validator suitable for If-Range.
func strongETag(header http.Header) (string, bool) {
	values := header.Values("ETag")
	if len(values) != 1 {
		return "", false
	}
	tag := strings.TrimSpace(values[0])
	if len(tag) < 2 || tag[0] != '"' || tag[len(tag)-1] != '"' {

View on GitHub (pinned to 7fd6ef3c07)

Solutions

  1. Confirm the server emits 'bytes start-end/total' with non-negative offsets.
  2. Fix middleware that mangles the range syntax.
  3. If you need suffix ranges, fetch the full resource instead of the partial path.
  4. Retry the request in case of transient corruption.

Example fix

// before
// Content-Range: bytes -1-99/500
// after
// Content-Range: bytes 0-99/500
Defensive patterns

Strategy: validation

Validate before calling

func nonNegativeRange(h string) bool {
	_, spec, _ := strings.Cut(h, " ")
	startEnd, _, _ := strings.Cut(spec, "/")
	s, e, _ := strings.Cut(startEnd, "-")
	start, err1 := strconv.ParseInt(s, 10, 64)
	end, err2 := strconv.ParseInt(e, 10, 64)
	return err1 == nil && err2 == nil && start >= 0 && end >= 0
}

Try / catch

cr, err := parseContentRange(header)
if err != nil {
	return fmt.Errorf("server sent negative offsets in %q: %w", header, err)
}

Prevention

When it happens

Trigger: openPartial or openNext received e.g. 'bytes -1-99/500' or 'bytes 0--1/500'.

Common situations: Server or proxy emitting suffix-range syntax with a stray dash; corrupted headers; test fixtures with negative values.

Related errors


AI-assisted analysis of larksuite/cli@7fd6ef3c07 (2026-09-04). Data as JSON: /api/errors/a17a748c366e7d3d. Report an issue: GitHub.