denoland/deno · error · RangeError

Out of range index

Error message

Out of range index

What it means

Buffer.prototype.fill() validates its start (and end) indices against the buffer's own length: start must satisfy 0 <= start <= buf.length and end must not exceed buf.length. This 'Out of range index' RangeError fires when start points past the end of the buffer - start is only pre-validated against buffer.constants.MAX_LENGTH (kMaxLength), not against this particular buffer's size, so a start larger than the buffer but smaller than 2 GiB reaches this check.

Source

Thrown at ext/node/polyfills/internal/buffer.mjs:2168

    val = val & 255;
  } else if (typeof val === "boolean") {
    val = Number(val);
  }

  if (typeof start === "string") {
    encoding = start;
    start = 0;
    end = this.length;
  }
  if (start !== undefined) {
    validateNumber(start, "start", 0, kMaxLength);
    if (end !== undefined) {
      validateNumber(end, "end", 0, this.length);
    }
  }

  if (start < 0 || this.length < start || this.length < end) {
    throw new RangeError("Out of range index");
  }
  if (end <= start) {
    return this;
  }
  start = start >>> 0;
  end = end === void 0 ? this.length : end >>> 0;
  if (!val) {
    val = 0;
  }
  let i;
  if (typeof val === "number") {
    // OOB check
    const byteLen = TypedArrayPrototypeGetByteLength(this);
    const fillLength = end - start;
    if (start > end || fillLength + start > byteLen) {
      throw new codes.ERR_BUFFER_OUT_OF_BOUNDS();
    }

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Clamp before calling: start = Math.min(start, buf.length); end = Math.min(end ?? buf.length, buf.length)
  2. Fill a subarray instead of passing offsets: buf.subarray(0, n).fill(0)
  3. Trace where the index came from - it is usually a stale length from a different buffer or an off-by-one
  4. Remember the signature fill(value[, start[, end]][, encoding]) so a string value plus encoding is not mistaken for indices

Example fix

// before
const buf = Buffer.alloc(3);
buf.fill(0, 5); // RangeError: Out of range index

// after
const start = Math.min(5, buf.length);
buf.fill(0, start);
Defensive patterns

Strategy: validation

Validate before calling

function safeFill(buf, value, start = 0, end = buf.length) {
  const s = Math.max(0, Math.min(start, buf.length));
  const e = Math.max(s, Math.min(end, buf.length));
  return buf.fill(value, s, e);
}

Try / catch

try {
  buf.fill(v, s, e);
} catch (err) {
  if (err instanceof RangeError) { /* clamp bounds to buf.length and retry */ }
  else throw err;
}

Prevention

When it happens

Trigger: buf.fill(value, start) with start > buf.length, e.g. Buffer.alloc(3).fill(0, 5); indices copied from a larger buffer; constants that assume a minimum buffer size; offsets reused after the buffer was reallocated smaller.

Common situations: Copy-pasting fill calls between buffers of different sizes; default offsets (e.g. HEADER = 16) hitting small test buffers; filling a subarray-sized region but passing the original buffer with stale offsets.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/355db2fd8eb25fb2. Report an issue: GitHub.