zloirock/core-js · error · RangeError

Wrong index

Error message

Wrong index

What it means

The DataView.prototype.get* fallback in core-js reads `count` bytes at byte `index`. toIndex coerces and validates the index; if index + count exceeds the view's byteLength, a RangeError 'Wrong index' is thrown, matching the spec's out-of-bounds read requirement.

Source

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

var packFloat64 = function (number) {
  return packIEEE754(number, 52, 8);
};

var addGetter = function (Constructor, key, getInternalState) {
  defineBuiltInAccessor(Constructor[PROTOTYPE], key, {
    configurable: true,
    get: function () {
      return getInternalState(this)[key];
    }
  });
};

var get = function (view, count, index, isLittleEndian) {
  var store = getInternalDataViewState(view);
  var intIndex = toIndex(index);
  var boolIsLittleEndian = !!isLittleEndian;
  if (intIndex + count > store.byteLength) throw new RangeError(WRONG_INDEX);
  var bytes = store.bytes;
  var start = intIndex + store.byteOffset;
  var pack = arraySlice(bytes, start, start + count);
  return boolIsLittleEndian ? pack : reverse(pack);
};

var set = function (view, count, index, conversion, value, isLittleEndian) {
  var store = getInternalDataViewState(view);
  var intIndex = toIndex(index);
  var pack = conversion(+value);
  var boolIsLittleEndian = !!isLittleEndian;
  if (intIndex + count > store.byteLength) throw new RangeError(WRONG_INDEX);
  var bytes = store.bytes;
  var start = intIndex + store.byteOffset;
  for (var i = 0; i < count; i++) bytes[start + i] = pack[boolIsLittleEndian ? i : count - i - 1];
};

if (!NATIVE_ARRAY_BUFFER) {

View on GitHub (pinned to 84e45fba09)

Solutions

  1. Clamp offsets: if (off + size <= view.byteLength) read.
  2. Check dataview.byteLength before each read and size the read to remaining bytes.
  3. Verify the buffer wasn't created with a truncated byteLength argument.
  4. Catch RangeError around reads when parsing untrusted-length data.

Example fix

// before
var v = dv.getFloat64(dv.byteLength - 4); // RangeError: Wrong index
// after
var v = dv.getFloat64(dv.byteLength - 8);
Defensive patterns

Strategy: validation

Validate before calling

function canRead(dv, offset, size){ return Number.isInteger(offset) && offset >= 0 && offset + size <= dv.byteLength; }
if (!canRead(dv, off, 8)) throw new RangeError('read out of bounds');

Try / catch

try { value = dv.getFloat64(off); } catch (e) { if (e instanceof RangeError && /Wrong index/.test(e.message)) return readTruncated(); throw e; }

Prevention

When it happens

Trigger: Calling dataView.getFloat64(off) / getUint32(off) etc. where off + elementSize > view.byteLength, or a negative/non-integer offset that toIndex rejects. Reached via the `bytes` accessor used by every DataView getX method.

Common situations: Hand-rolled binary parsers using fixed offsets against shorter buffers; off-by-one in loop bounds; forgetting that a DataView with byteLength limited at construction only exposes that slice; parsing truncated network/file payloads.

Related errors


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