{"record":{"id":"355db2fd8eb25fb2","repo":"denoland/deno","slug":"out-of-range-index","errorCode":null,"errorMessage":"Out of range index","messagePattern":"Out of range index","errorType":"validation","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/buffer.mjs","lineNumber":2168,"sourceCode":"    val = val & 255;\n  } else if (typeof val === \"boolean\") {\n    val = Number(val);\n  }\n\n  if (typeof start === \"string\") {\n    encoding = start;\n    start = 0;\n    end = this.length;\n  }\n  if (start !== undefined) {\n    validateNumber(start, \"start\", 0, kMaxLength);\n    if (end !== undefined) {\n      validateNumber(end, \"end\", 0, this.length);\n    }\n  }\n\n  if (start < 0 || this.length < start || this.length < end) {\n    throw new RangeError(\"Out of range index\");\n  }\n  if (end <= start) {\n    return this;\n  }\n  start = start >>> 0;\n  end = end === void 0 ? this.length : end >>> 0;\n  if (!val) {\n    val = 0;\n  }\n  let i;\n  if (typeof val === \"number\") {\n    // OOB check\n    const byteLen = TypedArrayPrototypeGetByteLength(this);\n    const fillLength = end - start;\n    if (start > end || fillLength + start > byteLen) {\n      throw new codes.ERR_BUFFER_OUT_OF_BOUNDS();\n    }\n","sourceCodeStart":2150,"sourceCodeEnd":2186,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/internal/buffer.mjs#L2150-L2186","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Clamp before calling: start = Math.min(start, buf.length); end = Math.min(end ?? buf.length, buf.length)","Fill a subarray instead of passing offsets: buf.subarray(0, n).fill(0)","Trace where the index came from - it is usually a stale length from a different buffer or an off-by-one","Remember the signature fill(value[, start[, end]][, encoding]) so a string value plus encoding is not mistaken for indices"],"exampleFix":"// before\nconst buf = Buffer.alloc(3);\nbuf.fill(0, 5); // RangeError: Out of range index\n\n// after\nconst start = Math.min(5, buf.length);\nbuf.fill(0, start);","handlingStrategy":"validation","validationCode":"function safeFill(buf, value, start = 0, end = buf.length) {\n  const s = Math.max(0, Math.min(start, buf.length));\n  const e = Math.max(s, Math.min(end, buf.length));\n  return buf.fill(value, s, e);\n}","typeGuard":null,"tryCatchPattern":"try {\n  buf.fill(v, s, e);\n} catch (err) {\n  if (err instanceof RangeError) { /* clamp bounds to buf.length and retry */ }\n  else throw err;\n}","preventionTips":["Derive indices from the buffer being filled, never from a sibling buffer","Route all fill/subarray bounds math through one helper","Property-test with random small buffer sizes to catch stale offsets"],"tags":["buffer","range-error","index-out-of-bounds"],"backgroundTag":"index-out-of-range","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}