{"record":{"id":"2dfbfa7c7bcbb3b1","repo":"denoland/deno","slug":"attempt-to-allocate-buffer-larger-than-maximum-siz","errorCode":null,"errorMessage":"Attempt to allocate Buffer larger than maximum size: 0x${kMaxLength} bytes","messagePattern":"Attempt to allocate Buffer larger than maximum size: 0x(.+?) bytes","errorType":"validation","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/buffer.mjs","lineNumber":519,"sourceCode":"\nfunction fromObject(obj) {\n  // deno-lint-ignore deno-internal/prefer-primordials\n  if (obj.length !== undefined || isAnyArrayBuffer(obj.buffer)) {\n    if (typeof obj.length !== \"number\") {\n      return createBuffer(0);\n    }\n\n    return fromArrayLike(obj);\n  }\n\n  if (obj.type === \"Buffer\" && ArrayIsArray(obj.data)) {\n    return fromArrayLike(obj.data);\n  }\n}\n\nfunction checked(length) {\n  if (length >= kMaxLength) {\n    throw new RangeError(\n      \"Attempt to allocate Buffer larger than maximum size: 0x\" +\n        NumberPrototypeToString(kMaxLength, 16) + \" bytes\",\n    );\n  }\n  return MathTrunc(length);\n}\n\nfunction SlowBuffer(length) {\n  if (!slowBufferWarningAlreadyEmitted) {\n    slowBufferWarningAlreadyEmitted = true;\n    process.emitWarning(slowBufferWarning, \"DeprecationWarning\", \"DEP0030\");\n  }\n  assertSize(length);\n  return _alloc(+length);\n}\n\nObjectSetPrototypeOf(SlowBuffer.prototype, Uint8ArrayPrototype);\n","sourceCodeStart":501,"sourceCodeEnd":537,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/internal/buffer.mjs#L501-L537","documentation":"checked(length) guards array-like-to-Buffer conversions (fromArrayLike, SlowBuffer) and throws a RangeError ('Attempt to allocate Buffer larger than maximum size: 0x...') at buffer.mjs:518-519 when length >= kMaxLength. In this polyfill kMaxLength is Number.MAX_SAFE_INTEGER (buffer.mjs:147), so it effectively fires only on degenerate computed lengths such as Infinity; on Node the ceiling is 2^32-1 and real oversized conversions throw there. It prevents a single allocation from exceeding the maximum Buffer size.","triggerScenarios":"Buffer.from(arrayLike) where arrayLike.length is Infinity or a corrupted huge number (sparse arrays with tampered length, broken .length getters); SlowBuffer(Infinity) or SlowBuffer with an overflowed computed size; feeding objects whose length getter returns garbage (divide-by-zero scaling) into Buffer.from.","commonSituations":"Protocols trusting a declared count and materializing arrays of that size; math errors producing Infinity sizes; code ported from platforms with different maxima; memory-pressure fallbacks that try to buffer everything at once.","solutions":["Validate the length before converting: require a finite, safe integer within buffer.constants.MAX_LENGTH","Chunk large conversions: convert array-likes in slices instead of one Buffer.from","Treat declared lengths from untrusted sources as hostile: cap them early with a clear error","For files and streams, use streaming reads (fs.createReadStream) rather than whole-entity buffers"],"exampleFix":"// before\nconst buf = Buffer.from(items); // items.length may be Infinity\n\n// after\nif (!Number.isSafeInteger(items.length) || items.length < 0 ||\n    items.length >= buffer.constants.MAX_LENGTH) {\n  throw new RangeError(`invalid source length: ${items.length}`);\n}\nconst buf = Buffer.from(items);","handlingStrategy":"validation","validationCode":"import buffer from 'node:buffer';\n\nfunction assertSourceLength(len) {\n  if (!Number.isSafeInteger(len) || len < 0 ||\n      len >= buffer.constants.MAX_LENGTH) {\n    throw new RangeError('invalid source length: ' + String(len));\n  }\n}\n\nassertSourceLength(items.length);\nconst buf = Buffer.from(items);","typeGuard":null,"tryCatchPattern":"try {\n  result = Buffer.from(source);\n} catch (err) {\n  if (err instanceof RangeError && /maximum size/.test(String(err.message))) {\n    result = Buffer.concat(chunkify(source)); // split and stream in chunks\n  } else {\n    throw err;\n  }\n}","preventionTips":["Distrust length-like properties from untrusted or tampered sources","Chunk big conversions instead of one Buffer.from call","Use fs streams for file-sized data; never materialize whole entities when streaming works"],"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"}