{"record":{"id":"43bb142d5b794827","repo":"copy/v86","slug":"length-doesn-t-match-header-real-header","errorCode":null,"errorMessage":"Length doesn't match header: real=... header=...","messagePattern":"Length doesn't match header: real=\\.\\.\\. header=\\.\\.\\.","errorType":"exception","errorClass":"StateLoadError","httpStatus":null,"severity":"error","filePath":"src/state.js","lineNumber":229,"sourceCode":"        }\n\n        const header_block = new Int32Array(state.buffer, state.byteOffset, 4);\n\n        if(header_block[STATE_INDEX_MAGIC] !== STATE_MAGIC)\n        {\n            throw new StateLoadError(\"Invalid header: \" + h(header_block[STATE_INDEX_MAGIC] >>> 0));\n        }\n\n        if(header_block[STATE_INDEX_VERSION] !== STATE_VERSION)\n        {\n            throw new StateLoadError(\n                    \"Version mismatch: dump=\" + header_block[STATE_INDEX_VERSION] +\n                    \" we=\" + STATE_VERSION);\n        }\n\n        if(check_length && header_block[STATE_INDEX_TOTAL_LEN] !== len)\n        {\n            throw new StateLoadError(\n                    \"Length doesn't match header: \" +\n                    \"real=\" + len + \" header=\" + header_block[STATE_INDEX_TOTAL_LEN]);\n        }\n\n        return header_block[STATE_INDEX_INFO_LEN];\n    }\n\n    function read_info_block(info_block_buffer)\n    {\n        const info_block = new TextDecoder().decode(info_block_buffer);\n        return JSON.parse(info_block);\n    }\n\n    if(new Uint32Array(state.buffer, 0, 1)[0] === ZSTD_MAGIC)\n    {\n        const ctx = cpu.zstd_create_ctx(state.length);\n\n        new Uint8Array(cpu.wasm_memory.buffer, cpu.zstd_get_src_ptr(ctx) >>> 0, state.length).set(state);","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/copy/v86/blob/180830d539dcc87db1a191febf6c914f516d102f/src/state.js#L211-L247","documentation":"The header records STATE_INDEX_TOTAL_LEN, the exact byte length the dump had when written. When check_length is true, read_state_header compares it with the actual buffer length and throws StateLoadError('Length doesn't match header: real=N header=M') on any mismatch, catching truncated, padded, or concatenated state buffers.","triggerScenarios":"Passing a Uint8Array to restore_state whose byteLength differs from the value stored at STATE_INDEX_TOTAL_LEN in the dump header — truncated download, extra bytes appended, slicing the wrong range out of a bigger blob, or byte-level corruption of the header.","commonSituations":"Interrupted downloads/partial uploads of state files; storage layers (localStorage, IndexedDB) altering or clipping blobs; manually slicing states out of a combined file with off-by-N errors.","solutions":["Verify the state file size matches what was originally saved (compare with the header value printed in the error)","Re-download/re-export the state file completely and retry","If the state is embedded in a larger blob, slice exactly the header-reported length","Check the storage/transfer path for corruption (compare checksums before and after)"],"exampleFix":"// before\nemulator.restore_state(bigBlob.subarray(offset)); // length may not match header\n// after\nconst headerLen = new Int32Array(bigBlob.buffer, bigBlob.byteOffset, 4)[3];\nemulator.restore_state(bigBlob.subarray(offset, offset + headerLen));","handlingStrategy":"validation","validationCode":"function headerLengthMatches(buf) {\n    if (!(buf instanceof Uint8Array) || buf.byteLength < 12) return false;\n    const h = new Int32Array(buf.buffer, buf.byteOffset, 4);\n    return h[3] === buf.byteLength; // STATE_INDEX_TOTAL_LEN === len\n}\nif (!headerLengthMatches(state)) throw new Error(\"state file size mismatch\");","typeGuard":null,"tryCatchPattern":"try {\n    emulator.restore_state(state);\n} catch (e) {\n    if (e instanceof StateLoadError && /Length doesn't match header/.test(e.message)) {\n    reacquireStateFile(); // re-download / re-export\n    } else throw e;\n}","preventionTips":["Compare file sizes (or checksums) after download/transfer","Slice embedded states using the header-reported total length","Avoid localStorage/IndexedDB paths that may alter blob sizes","Verify transfer integrity with checksums"],"tags":["state","validation","truncated-data"],"backgroundTag":"state-load-failed","analyzedSha":"180830d539dcc87db1a191febf6c914f516d102f","analyzedAt":"2026-08-31T22:39:37.599Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}