{"record":{"id":"11b51b8adb7a3290","repo":"denoland/deno","slug":"the-value-length-is-invalid-for-option-size","errorCode":null,"errorMessage":"The value \"${length}\" is invalid for option \"size\"","messagePattern":"The value \"(.+?)\" is invalid for option \"size\"","errorType":"validation","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/buffer.mjs","lineNumber":233,"sourceCode":"    }\n    return TypedArrayPrototypeGetBuffer(this);\n  },\n});\n\nObjectDefineProperty(Buffer.prototype, \"offset\", {\n  __proto__: null,\n  enumerable: true,\n  get: function () {\n    if (!BufferIsBuffer(this)) {\n      return void 0;\n    }\n    return TypedArrayPrototypeGetByteOffset(this);\n  },\n});\n\nfunction createBuffer(length) {\n  if (length > kMaxLength) {\n    throw new RangeError(\n      'The value \"' + length + '\" is invalid for option \"size\"',\n    );\n  }\n\n  return new FastBuffer(length);\n}\n\n/**\n * @param {ArrayBufferLike} O\n * @returns {boolean}\n */\nfunction isDetachedBuffer(O) {\n  if (isSharedArrayBuffer(O)) {\n    return false;\n  }\n  return ArrayBufferPrototypeGetDetached(O);\n}\n","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/internal/buffer.mjs#L215-L251","documentation":"createBuffer(length) - the allocator behind Buffer.allocUnsafe, allocUnsafeSlow, and internal pool paths - throws a plain RangeError ('The value \"N\" is invalid for option \"size\"') at buffer.mjs:233 when length exceeds kMaxLength. In this polyfill kMaxLength is Number.MAX_SAFE_INTEGER (buffer.mjs:147), so only degenerate non-finite or astronomic values reach it; on Node.js the cap is 2^32-1, so very large legitimate-looking allocations throw there. It is a pre-allocation guard, not an out-of-memory error.","triggerScenarios":"Buffer.allocUnsafe(Infinity) - a computed size that degenerated to Infinity (division by zero, overflowed arithmetic); sizes derived from untrusted input such as a Content-Length or file length parsed as float and multiplied without bounds checks; code written against a platform with a larger maximum buffer size run where limits are stricter.","commonSituations":"Streaming code that falls back to 'read the whole file' with a scaled size; protocol parsers trusting a header-declared length; numeric coercion bugs turning NaN or Infinity into the size argument.","solutions":["Validate size before allocating: Number.isSafeInteger(size) && size >= 0 && size <= buffer.constants.MAX_LENGTH","Clamp or reject untrusted lengths (headers, file sizes) at the trust boundary before they reach Buffer APIs","Stream data with readable streams in chunks instead of one giant buffer","Fix the arithmetic that produced Infinity or overflow (guard divisions, use integer math)"],"exampleFix":"// before\nconst buf = Buffer.allocUnsafe(totalBytes); // totalBytes may be Infinity\n\n// after\nif (!Number.isSafeInteger(totalBytes) || totalBytes < 0 ||\n    totalBytes > buffer.constants.MAX_LENGTH) {\n  throw new RangeError(`invalid size: ${totalBytes}`);\n}\nconst buf = Buffer.allocUnsafe(totalBytes);","handlingStrategy":"validation","validationCode":"import buffer from 'node:buffer';\n\nfunction assertAllocSize(size) {\n  if (!Number.isSafeInteger(size) || size < 0 ||\n      size > buffer.constants.MAX_LENGTH) {\n    throw new RangeError('invalid allocation size: ' + String(size));\n  }\n}\n\nassertAllocSize(totalBytes);\nconst buf = Buffer.allocUnsafe(totalBytes);","typeGuard":null,"tryCatchPattern":"try {\n  buf = Buffer.allocUnsafe(requestedSize);\n} catch (err) {\n  if (err instanceof RangeError) {\n    throw new Error('requested size too large: ' + requestedSize);\n  }\n  throw err;\n}","preventionTips":["Never feed Content-Length, file sizes, or header counts into allocation without a cap","Stream large payloads in fixed-size chunks instead of one allocation","Guard arithmetic that computes sizes (no division by zero, integer math)"],"tags":["buffer","memory-allocation","range-error","node-compat","input-validation"],"backgroundTag":"buffer-size-limit-exceeded","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}