{"record":{"id":"ad48f7fbfba5a331","repo":"lutzroeder/netron","slug":"unexpected-end-of-file-expected-this-offset","errorCode":null,"errorMessage":"Unexpected end of file. Expected ${this._offset - this._length} more bytes. The file might be corrupted.","messagePattern":"Unexpected end of file\\. Expected (.+?) more bytes\\. The file might be corrupted\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"source/hdf5.js","lineNumber":516,"sourceCode":"        this._offsetSize = offsetSize;\n        this._lengthSize = lengthSize;\n    }\n\n    get position() {\n        return this._position + this._offset;\n    }\n\n    take(offset) {\n        const position = this._offset + this._position;\n        this.skip(offset);\n        return position;\n    }\n\n    seek(position) {\n        this._offset = position;\n        this._position = 0;\n        if (this._offset > this._length) {\n            throw new Error(`Unexpected end of file. Expected ${this._offset - this._length} more bytes. The file might be corrupted.`);\n        }\n    }\n\n    skip(offset) {\n        this._position += offset;\n        if (this._offset + this._position > this._buffer.length) {\n            throw new hdf5.Error(`Unexpected end of file. Expected ${this._offset + this._position - this._buffer.length} more bytes. The file might be corrupted.`);\n        }\n    }\n\n    align(mod) {\n        if (this._position % mod !== 0) {\n            this._position = (Math.floor(this._position / mod) + 1) * mod;\n        }\n    }\n\n    peek(length) {\n        const position = this._offset + this._position;","sourceCodeStart":498,"sourceCodeEnd":534,"githubUrl":"https://github.com/lutzroeder/netron/blob/d8a543f5f847ef596aa58858d6adcf5d75545f7f/source/hdf5.js#L498-L534","documentation":"During random access into the HDF5 file image, the reader seeks to a byte offset that is past the end of the data it holds (this._offset > this._length) and aborts. Every seek past EOF is treated as evidence of structural corruption, because in a well-formed HDF5 file all offsets stored in headers point inside the file.","triggerScenarios":"Calling seek(position) with an offset beyond the buffer/stream length — typically indirectly, when a parsed object header, B-tree, or local heap yields an address larger than the file size. This variant guards the buffered reader (compare with the stream-based seek at line ~584) and fires after this._offset is assigned.","commonSituations":"Truncated downloads of .h5/.hdf5 files, files corrupted in transfer, reading a non-HDF5 file through the HDF5 front-end (magic mismatch misleads the parser into garbage offsets), or a user-generated file with wrong superblock version assumptions.","solutions":["Confirm the file is a valid HDF5 file (check the \\x89HDF\\r\\n\\x1a\\n signature at byte 0) before opening it.","Re-download or re-export the file and compare checksums; truncation is the most common cause.","Reproduce with the official h5dump/h5ls tools — if they also fail, the file itself is damaged.","If the file is valid, report a parser bug with the file to the library maintainers."],"exampleFix":"null","handlingStrategy":"validation","validationCode":"function canSeek(reader, position) {\n  return Number.isInteger(position) && position >= 0 && position <= reader._length;\n}\nif (canSeek(reader, targetOffset)) reader.seek(targetOffset); else handleCorruptFile();","typeGuard":"function isValidHdf5Offset(pos, fileLength) {\n  return typeof pos === 'number' && pos >= 0 && pos < fileLength;\n}","tryCatchPattern":"try {\n  reader.seek(addr);\n} catch (e) {\n  if (/Unexpected end of file/.test(e.message)) {\n    reportCorruptOrTruncatedFile(file);\n  } else throw e;\n}","preventionTips":["Verify the \\x89HDF\\r\\n\\x1a\\n signature before opening.","Transfer HDF5 files with checksum verification to avoid truncation.","Wrap the whole open/parse flow in a single try/catch that surfaces 'file may be corrupted' UX."],"tags":["hdf5","eof","seek","corrupt-file","truncated-file"],"backgroundTag":"corrupt-file-unexpected-eof","analyzedSha":"d8a543f5f847ef596aa58858d6adcf5d75545f7f","analyzedAt":"2026-08-27T19:21:42.082Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}