ipfs/kubo · error

cannot specify negative 'count'

Error message

cannot specify negative 'count'

What it means

Guard in the `ipfs files write` handler: before wrapping the input reader with io.LimitReader, it rejects a --count option that was parsed as a negative int64. It fires on input validation, not on file I/O — the write has not started yet.

Source

Thrown at core/commands/files.go:884

		filen, err := rfd.Size()
		if err != nil {
			return err
		}

		if int64(offset) > filen {
			return fmt.Errorf("offset was past end of file (%d > %d)", offset, filen)
		}

		_, err = rfd.Seek(int64(offset), io.SeekStart)
		if err != nil {
			return err
		}

		var r io.Reader = &contextReaderWrapper{R: rfd, ctx: req.Context}
		count, found := req.Options[filesCountOptionName].(int64)
		if found {
			if count < 0 {
				return fmt.Errorf("cannot specify negative 'count'")
			}
			r = io.LimitReader(r, int64(count))
		}
		return res.Emit(r)
	},
}

type contextReader interface {
	CtxReadFull(context.Context, []byte) (int, error)
}

type contextReaderWrapper struct {
	R   contextReader
	ctx context.Context
}

func (crw *contextReaderWrapper) Read(b []byte) (int, error) {
	return crw.R.CtxReadFull(crw.ctx, b)

View on GitHub (pinned to 329838acdf)

Solutions

  1. Pass a non-negative --count to `ipfs files write`, or omit the option to write the entire stream.
  2. Fix the calling script that computes count so it cannot produce a negative value (e.g. clamp with max(0, n)).
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at core/commands/files.go:884 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of ipfs/kubo@329838acdf (2026-09-03). Data as JSON: /api/errors/67b4d13dea950b8a. Report an issue: GitHub.