denoland/deno · error · RangeError

The value "${length}" is invalid for option "size"

Error message

The value "${length}" is invalid for option "size"

What it means

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.

Source

Thrown at ext/node/polyfills/internal/buffer.mjs:233

    }
    return TypedArrayPrototypeGetBuffer(this);
  },
});

ObjectDefineProperty(Buffer.prototype, "offset", {
  __proto__: null,
  enumerable: true,
  get: function () {
    if (!BufferIsBuffer(this)) {
      return void 0;
    }
    return TypedArrayPrototypeGetByteOffset(this);
  },
});

function createBuffer(length) {
  if (length > kMaxLength) {
    throw new RangeError(
      'The value "' + length + '" is invalid for option "size"',
    );
  }

  return new FastBuffer(length);
}

/**
 * @param {ArrayBufferLike} O
 * @returns {boolean}
 */
function isDetachedBuffer(O) {
  if (isSharedArrayBuffer(O)) {
    return false;
  }
  return ArrayBufferPrototypeGetDetached(O);
}

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Validate size before allocating: Number.isSafeInteger(size) && size >= 0 && size <= buffer.constants.MAX_LENGTH
  2. Clamp or reject untrusted lengths (headers, file sizes) at the trust boundary before they reach Buffer APIs
  3. Stream data with readable streams in chunks instead of one giant buffer
  4. Fix the arithmetic that produced Infinity or overflow (guard divisions, use integer math)

Example fix

// before
const buf = Buffer.allocUnsafe(totalBytes); // totalBytes may be Infinity

// after
if (!Number.isSafeInteger(totalBytes) || totalBytes < 0 ||
    totalBytes > buffer.constants.MAX_LENGTH) {
  throw new RangeError(`invalid size: ${totalBytes}`);
}
const buf = Buffer.allocUnsafe(totalBytes);
Defensive patterns

Strategy: validation

Validate before calling

import buffer from 'node:buffer';

function assertAllocSize(size) {
  if (!Number.isSafeInteger(size) || size < 0 ||
      size > buffer.constants.MAX_LENGTH) {
    throw new RangeError('invalid allocation size: ' + String(size));
  }
}

assertAllocSize(totalBytes);
const buf = Buffer.allocUnsafe(totalBytes);

Try / catch

try {
  buf = Buffer.allocUnsafe(requestedSize);
} catch (err) {
  if (err instanceof RangeError) {
    throw new Error('requested size too large: ' + requestedSize);
  }
  throw err;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/11b51b8adb7a3290. Report an issue: GitHub.