{"record":{"id":"a615bf707cec1df7","repo":"denoland/deno","slug":"err-out-of-range-a615bf","errorCode":"ERR_OUT_OF_RANGE","errorMessage":"The value of \"offset\" is out of range. It must be >= 0. Received ${offset}","messagePattern":"The value of \"offset\" is out of range\\. It must be >= 0\\. Received (.+?)","errorType":"validation","errorClass":"ERR_OUT_OF_RANGE","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/fs/utils.mjs","lineNumber":858,"sourceCode":"    return +time;\n  }\n  if (NumberIsFinite(time)) {\n    if (time < 0) {\n      return DateNow() / 1000;\n    }\n    return time;\n  }\n  if (isDate(time)) {\n    // Convert to 123.456 UNIX timestamp\n    return DatePrototypeGetTime(time) / 1000;\n  }\n  throw new ERR_INVALID_ARG_TYPE(name, [\"Date\", \"Time in seconds\"], time);\n}\n\nexport const validateOffsetLengthRead = hideStackFrames(\n  (offset, length, bufferLength) => {\n    if (offset < 0) {\n      throw new ERR_OUT_OF_RANGE(\"offset\", \">= 0\", offset);\n    }\n    if (length < 0) {\n      throw new ERR_OUT_OF_RANGE(\"length\", \">= 0\", length);\n    }\n    if (offset + length > bufferLength) {\n      throw new ERR_OUT_OF_RANGE(\n        \"length\",\n        `<= ${bufferLength - offset}`,\n        length,\n      );\n    }\n  },\n);\n\nexport const validateOffsetLengthWrite = hideStackFrames(\n  (offset, length, byteLength) => {\n    if (offset > byteLength) {\n      throw new ERR_OUT_OF_RANGE(\"offset\", `<= ${byteLength}`, offset);","sourceCodeStart":840,"sourceCodeEnd":876,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/internal/fs/utils.mjs#L840-L876","documentation":"validateOffsetLengthRead guards the destination window of fs.read(fd, buffer, offset, length, position): offset is where fetched bytes start landing inside buffer and cannot be negative. A negative offset is rejected before any I/O with ERR_OUT_OF_RANGE.","triggerScenarios":"fs.read(fd, buf, -1, 10, 0); offset computed as start - total going negative once the remaining bytes shrink; argument-order swaps where position lands in the offset slot.","commonSituations":"Hand-rolled chunked readers computing offsets from bytes-left arithmetic; code ported from C read(2) with different parameter meaning; off-by-one bugs after switching to Buffer.subarray views.","solutions":["Ensure offset >= 0 — for whole-buffer reads it is simply 0.","Re-check the signature: fs.read(fd, buffer, offset, length, position).","Derive length from the view actually passed: buf.length - offset."],"exampleFix":"// before\nfs.readSync(fd, chunk, start - total, chunk.length, null); // start-total < 0 on short input\n\n// after\nconst off = Math.max(0, start - total);\nfs.readSync(fd, chunk, off, chunk.length - off, null);","handlingStrategy":"validation","validationCode":"function checkReadWindow(offset, length, buf) {\n  if (!(offset >= 0)) throw new RangeError(`offset must be >= 0, got ${offset}`);\n  if (!(length >= 0)) throw new RangeError(`length must be >= 0, got ${length}`);\n  if (offset + length > buf.length) throw new RangeError(`offset+length exceeds buffer of ${buf.length}`);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp computed offsets with Math.max(0, ...).","Prefer fs.read with no offset/length — defaults fill the whole buffer.","Re-verify the argument order (fd, buffer, offset, length, position) when porting read(2) code."],"tags":["filesystem","read","buffer","offset","out-of-range"],"backgroundTag":"buffer-offset-length-out-of-range","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}