zloirock/core-js · error · RangeError

Wrong length

Error message

Wrong length

What it means

After validating the offset, the polyfilled DataView constructor validates the (possibly defaulted) byteLength: if byteOffset + byteLength exceeds buffer.byteLength, it throws RangeError WRONG_LENGTH ('Wrong length'). This enforces that the view stays within the buffer, per spec.

Source

Thrown at packages/core-js/internals/array-buffer.js:129

      byteLength: byteLength
    });
    if (!DESCRIPTORS) {
      this.byteLength = byteLength;
      this.detached = false;
    }
  };

  ArrayBufferPrototype = $ArrayBuffer[PROTOTYPE];

  $DataView = function DataView(buffer, byteOffset, byteLength) {
    anInstance(this, DataViewPrototype);
    anInstance(buffer, ArrayBufferPrototype);
    var bufferState = getInternalArrayBufferState(buffer);
    var bufferLength = bufferState.byteLength;
    var offset = toIntegerOrInfinity(byteOffset);
    if (offset < 0 || offset > bufferLength) throw new RangeError('Wrong offset');
    byteLength = byteLength === undefined ? bufferLength - offset : toIndex(byteLength);
    if (offset + byteLength > bufferLength) throw new RangeError(WRONG_LENGTH);
    setInternalState(this, {
      type: DATA_VIEW,
      buffer: buffer,
      byteLength: byteLength,
      byteOffset: offset,
      bytes: bufferState.bytes
    });
    if (!DESCRIPTORS) {
      this.buffer = buffer;
      this.byteLength = byteLength;
      this.byteOffset = offset;
    }
  };

  DataViewPrototype = $DataView[PROTOTYPE];

  if (DESCRIPTORS) {
    addGetter($ArrayBuffer, 'byteLength', getInternalArrayBufferState);

View on GitHub (pinned to 84e45fba09)

Solutions

  1. Compute length from remaining bytes: Math.min(len, buf.byteLength - off).
  2. Validate off + len <= buf.byteLength before constructing.
  3. Sanitize lengths parsed from external data.
  4. Catch RangeError to translate into an application-level 'malformed payload' error.

Example fix

// before
var dv = new DataView(buf, 0, buf.byteLength + 16); // RangeError: Wrong length
// after
var dv = new DataView(buf, 0, buf.byteLength);
Defensive patterns

Strategy: validation

Validate before calling

function safeDataView(buf, off, len){ if (off + len > buf.byteLength) len = buf.byteLength - off; return new DataView(buf, off, len); }

Try / catch

try { dv = new DataView(buf, off, len); } catch (e) { if (e instanceof RangeError && /Wrong length/.test(e.message)) { len = buf.byteLength - off; dv = new DataView(buf, off, Math.max(0, len)); } else throw e; }

Prevention

When it happens

Trigger: new DataView(buf, off, len) where off + len > buf.byteLength, or explicit byteLength exceeding remaining space after the offset.

Common situations: Sizing views from length fields read out of corrupt/truncated payloads; arithmetic mistakes combining offset+length; code assuming length is absolute buffer size rather than view size.

Related errors


AI-assisted analysis of zloirock/core-js@84e45fba09 (2026-08-30). Data as JSON: /api/errors/6f93c67ceb4bc65d. Report an issue: GitHub.