{"record":{"id":"b112fa2d811bed54","repo":"lutzroeder/netron","slug":"expected-this-position-length-this-length-b112fa","errorCode":null,"errorMessage":"Expected ${this._position + length - this._length} more bytes. The file might be corrupted. Unexpected end of file.","messagePattern":"Expected (.+?) more bytes\\. The file might be corrupted\\. Unexpected end of file\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"source/protobuf.js","lineNumber":633,"sourceCode":"    }\n\n    varint() {\n        let value = 0n;\n        let shift = 0n;\n        for (let i = 0; i < 10 && this._position < this._length; i++) {\n            const byte = this.byte();\n            value |= BigInt(byte & 0x7F) << shift;\n            if ((byte & 0x80) === 0) {\n                return value;\n            }\n            shift += 7n;\n        }\n        throw new protobuf.Error('Invalid varint value.');\n    }\n\n    _fill(length) {\n        if (this._position + length > this._length) {\n            throw new Error(`Expected ${this._position + length - this._length} more bytes. The file might be corrupted. Unexpected end of file.`);\n        }\n        if (!this._buffer || this._position < this._offset || this._position + length > this._offset + this._buffer.length) {\n            this._offset = this._position;\n            this._stream.seek(this._offset);\n            const size = Math.min(0x10000000, this._length - this._offset);\n            this._buffer = this._stream.read(size);\n            this._view = new DataView(this._buffer.buffer, this._buffer.byteOffset, this._buffer.byteLength);\n        }\n        const position = this._position;\n        this._position += length;\n        return position - this._offset;\n    }\n};\n\nprotobuf.TextReader = class {\n\n    static open(data) {\n        if (data) {","sourceCodeStart":615,"sourceCodeEnd":651,"githubUrl":"https://github.com/lutzroeder/netron/blob/d8a543f5f847ef596aa58858d6adcf5d75545f7f/source/protobuf.js#L615-L651","documentation":"Error thrown by protobuf.BinaryReader._fill when a requested read of 'length' bytes at the current position would exceed the underlying stream's declared length. _fill is invoked by every buffered read primitive, so this is the streaming counterpart of the memory-reader overrun check and reports exactly how many bytes are missing.","triggerScenarios":"Calling any read (uint32, bytes, string, fixed64, ...) whose wire-format length extends past _length — e.g. a length-delimited field claims 1000 bytes but only 10 remain in the stream.","commonSituations":"Truncated model files, mismatched length prefixes caused by decoding with the wrong schema or field order, or a stream length computed from a wrong header value.","solutions":["Validate the file/stream length against the container header before decoding.","Ensure the schema version used for decoding matches the encoder's (field ordering affects length parsing).","Wrap decode in try-catch and report the missing-byte count to pinpoint the corrupt section."],"exampleFix":"// before\nconst field = reader.bytes(); // length-delimited read\n\n// after — validate declared length before reading\nconst len = reader.uint32();\nif (reader.position + len > reader.length) {\n    throw new Error(`Field claims ${len} bytes, only ${reader.length - reader.position} remain`);\n}\nconst field = reader.bytes(len);","handlingStrategy":"validation","validationCode":"if (reader.position + expectedLength > reader.length) {\n    throw new Error(`Declared section of ${expectedLength} bytes exceeds stream bounds`);\n}","typeGuard":null,"tryCatchPattern":"try { const data = reader.bytes(); } catch (e) { if (/more bytes/.test(e.message)) throw new Error('Corrupted length-delimited field in model file'); throw e; }","preventionTips":["Cross-check container-level length fields with actual stream length before decoding.","Keep encoder and decoder schema versions in lockstep.","Catch and log the missing-byte count to identify which section is corrupt."],"tags":["protobuf","binary-reader","truncated-file","bounds-check"],"backgroundTag":"unexpected-end-of-file","analyzedSha":"d8a543f5f847ef596aa58858d6adcf5d75545f7f","analyzedAt":"2026-08-27T19:21:42.082Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}