{"record":{"id":"82d792a399bc3b9f","repo":"copy/v86","slug":"invalid-length","errorCode":null,"errorMessage":"Invalid length: ","messagePattern":"Invalid length: ","errorType":"exception","errorClass":"StateLoadError","httpStatus":null,"severity":"error","filePath":"src/state.js","lineNumber":210,"sourceCode":"\n    dbg_log(\"State: json size \" + (info_block.byteLength >> 10) + \"k\");\n    dbg_log(\"State: Total buffers size \" + (buffer_block.byteLength >> 10) + \"k\");\n\n    return result;\n}\n\n/* @param {CPU} cpu */\nexport function restore_state(cpu, state)\n{\n    state = new Uint8Array(state);\n\n    function read_state_header(state, check_length)\n    {\n        const len = state.length;\n\n        if(len < STATE_INFO_BLOCK_START)\n        {\n            throw new StateLoadError(\"Invalid length: \" + len);\n        }\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        {","sourceCodeStart":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/copy/v86/blob/180830d539dcc87db1a191febf6c914f516d102f/src/state.js#L192-L228","documentation":"read_state_header validates saved-state buffers before any restore. A state buffer shorter than STATE_INFO_BLOCK_START cannot even contain the mandatory header, so a StateLoadError('Invalid length: N') is thrown reporting the actual byte length received.","triggerScenarios":"Calling restore_state/state load APIs with a Uint8Array whose byteLength < STATE_INFO_BLOCK_START — e.g. an empty or truncated file, reading a URL that returned an error page, or passing the wrong (much smaller) buffer.","commonSituations":"Downloaded state file truncated (network error, interrupted download); server returned an HTML 404/500 page saved as the state file; user picked the wrong file; state loaded from localStorage that was never written.","solutions":["Check state.byteLength >= STATE_INFO_BLOCK_START (and nonzero) before calling the restore API","Re-export/re-download the state file and verify its size matches the original dump","Ensure the file/URL actually serves the v86 state binary, not an error page (check content-type/size)","Restore from a known-good backup state"],"exampleFix":"// before\nemulator.restore_state(stateBuffer); // throws if truncated\n// after\nconst STATE_INFO_BLOCK_START = 12;\nif (!stateBuffer || stateBuffer.byteLength < STATE_INFO_BLOCK_START) {\n    throw new Error(\"state file too small/truncated: \" + (stateBuffer?.byteLength ?? 0));\n}\nemulator.restore_state(stateBuffer);","handlingStrategy":"validation","validationCode":"function isPlausibleState(buf) {\n    return buf instanceof Uint8Array && buf.byteLength >= 12; // >= STATE_INFO_BLOCK_START\n}\nif (!isPlausibleState(state)) throw new Error(\"state truncated/empty: \" + (state?.byteLength ?? 0));","typeGuard":"function isStateBuffer(x) {\n    return x instanceof Uint8Array && x.byteLength >= 12;\n}","tryCatchPattern":"try {\n    emulator.restore_state(state);\n} catch (e) {\n    if (e instanceof StateLoadError && /Invalid length/.test(e.message)) {\n    promptUserToReexportState();\n    } else throw e;\n}","preventionTips":["Check file size > 0 and ≥ header size before restore","Verify downloads completed (content-length comparison)","Never save states from error-page responses","Keep verified backups of important states"],"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"}