{"record":{"id":"b67dace5be4a0abe","repo":"denoland/deno","slug":"err-index-out-of-range","errorCode":"ERR_INDEX_OUT_OF_RANGE","errorMessage":"Index out of range","messagePattern":"Index out of range","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/buffer.mjs","lineNumber":1269,"sourceCode":"function hexIndexOutOfRange() {\n  const err = new RangeError(\"Index out of range\");\n  err.code = \"ERR_OUT_OF_RANGE\";\n  return err;\n}\n\nBuffer.prototype.hexSlice = function hexSlice(start, end) {\n  let byteLength = TypedArrayPrototypeGetByteLength(this);\n  // Index semantics replicate Node's C++ StringSlice: zero-length receivers\n  // (including detached) return \"\" before any validation, ToInteger\n  // coercion, negative index throws \"Index out of range\", end < start\n  // clamps to empty, and only a forward range with end > length throws.\n  if (byteLength === 0) {\n    return \"\";\n  }\n  start = start === undefined ? 0 : MathTrunc(Number(start)) || 0;\n  end = end === undefined ? byteLength : MathTrunc(Number(end)) || 0;\n  if (start < 0 || end < 0) {\n    throw hexIndexOutOfRange();\n  }\n  if (end <= start) {\n    return \"\";\n  }\n  // Re-read: argument coercion can run user code that resizes or detaches\n  // the underlying buffer (detached views report length 0).\n  byteLength = TypedArrayPrototypeGetByteLength(this);\n  if (end > byteLength) {\n    throw hexIndexOutOfRange();\n  }\n  if (end - start > kStringMaxLength / 2) {\n    throw genericNodeError(\n      `Cannot create a string longer than 0x${\n        NumberPrototypeToString(kStringMaxLength, 16)\n      } characters`,\n      { code: \"ERR_STRING_TOO_LONG\" },\n    );\n  }","sourceCodeStart":1251,"sourceCodeEnd":1287,"githubUrl":"https://github.com/denoland/deno/blob/a961cdec3b1948844414ebeecc697dcad76df2d1/ext/node/polyfills/internal/buffer.mjs#L1251-L1287","documentation":"Buffer hex-encoding (toString('hex', start, end)) validates slice bounds: negative start or end values are out of range because slice indices must be non-negative byte offsets. hexIndexOutOfRange() throws ERR_INDEX_OUT_OF_RANGE to match Node's behavior rather than silently clamping.","triggerScenarios":"Calling buf.toString('hex', start, end) with a negative start or a negative end — e.g. end computed as buf.length - extra where extra > length, or -1 passed as an end sentinel.","commonSituations":"Off-by-one arithmetic producing negative end; porting code that used negative indices like Python/JS array slicing; uninitialized length variables.","solutions":["Clamp indices before the call: start = Math.max(0, start); end = Math.max(0, end).","Fix the index arithmetic so start/end are non-negative byte offsets within [0, byteLength].","Coerce with MathTrunc and validate user-supplied indices before use."],"exampleFix":"// before\nconst hex = buf.toString('hex', 0, buf.length - 8); // negative when short\n// after\nconst end = Math.max(0, buf.byteLength - 8);\nconst hex = buf.toString('hex', 0, end);","handlingStrategy":"validation","validationCode":"function clampHexRange(buf, start = 0, end = buf.byteLength) {\n  start = Math.max(0, Math.trunc(start) || 0);\n  end = Math.max(0, Math.trunc(end) || 0);\n  return [start, Math.min(end, buf.byteLength)];\n}\nconst [s, e] = clampHexRange(buf, userStart, userEnd);\nconst hex = buf.toString('hex', s, e);","typeGuard":"function isSafeIndex(v) {\n  return Number.isInteger(v) && v >= 0;\n}","tryCatchPattern":"try {\n  hex = buf.toString('hex', start, end);\n} catch (err) {\n  if (err.code === 'ERR_INDEX_OUT_OF_RANGE') {\n    [start, end] = clampHexRange(buf, start, end);\n    hex = buf.toString('hex', start, end);\n  }\n}","preventionTips":["Clamp all slice indices to [0, byteLength] before calling toString.","Avoid negative-index arithmetic styles from other languages (Python slicing).","Guard computed ends like length - n with Math.max(0, ...)."],"tags":["buffer","node-compat","validation"],"backgroundTag":"index-out-of-range","analyzedSha":"a961cdec3b1948844414ebeecc697dcad76df2d1","analyzedAt":"2026-09-03T14:18:07.398Z","contentChangedAt":"2026-09-03T14:18:07.398Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}