lutzroeder/netron · error · Error

Expected ${position + length - this._length} more bytes. The

Error message

Expected ${position + length - this._length} more bytes. The file might be corrupted. Unexpected end of file.

What it means

take(length) is the primitive that reserves length bytes at the current position; it throws when position + length runs past the end of the file image. Higher-level readers (variable-length integers, strings, object headers) all funnel through take(), so this error usually surfaces while decoding a header whose declared size does not fit in the remaining bytes.

Source

Thrown at source/hdf5.js:667

    float64() {
        const position = this.take(8);
        return this._view.getFloat64(position, true);
    }

    size(terminator) {
        const position = this._position;
        let size = 0;
        while (this.byte() !== terminator) {
            size++;
        }
        this._position = position;
        return size;
    }

    take(length) {
        const position = this.position;
        if (position + length > this._length) {
            throw new Error(`Expected ${position + length - this._length} more bytes. The file might be corrupted. Unexpected end of file.`);
        }
        if (!this._view || position < this._window || position + length > this._window + this._view.byteLength) {
            this._window = position;
            const current = this._stream.position;
            this._stream.seek(this._window);
            const buffer = this._stream.read(Math.min(0x100, this._length - this._window)).slice();
            this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
            this._stream.seek(current);
        }
        this._position += length;
        return position - this._window;
    }
};

hdf5.SymbolTableNode = class {

    constructor(reader, offset) {
        const position = reader.position;

View on GitHub (pinned to d8a543f5f8)

Solutions

  1. Verify the file opens in the official HDF5 tools; if not, restore/re-export the file.
  2. Ensure you are passing the raw, untruncated ArrayBuffer/File (no slicing, no compression) to the reader.
  3. Check the file's HDF5 library version (superblock version field) against what this library supports and re-export with an older format if needed.
  4. If the file is valid and supported, file an issue with a minimal reproducing file.

Example fix

null
Defensive patterns

Strategy: validation

Validate before calling

function canTake(reader, length) {
  const pos = reader.position;
  return length >= 0 && pos + length <= reader._length;
}
if (canTake(reader, headerSize)) reader.take(headerSize); else throw new Error('Truncated HDF5 header');

Type guard

function fitsInFile(position, length, fileLength) {
  return position >= 0 && length >= 0 && position + length <= fileLength;
}

Try / catch

try {
  const bytes = reader.take(n);
} catch (e) {
  if (/Unexpected end of file/.test(e.message)) {
    // abort parse, flag file as corrupt
  } else throw e;
}

Prevention

When it happens

Trigger: Any read that requests more bytes than remain: parsing an object header continuation, a symbol table message, a data layout message, or an attribute whose stated length extends past EOF. It also fires when a length field decoded from earlier garbage (e.g. wrong-endian or version-mismatched parse) produces an oversized take(n).

Common situations: Truncated HDF5 files (interrupted writes are common with HDF5), files that exceed the reader's supported HDF5 version (later versions add header fields that shift offsets and inflate subsequent length reads), or plain non-HDF5 binary data fed to the parser.

Related errors


AI-assisted analysis of lutzroeder/netron@d8a543f5f8 (2026-08-27). Data as JSON: /api/errors/245851c3cdef599c. Report an issue: GitHub.