{"record":{"id":"48b33b05fd47e28c","repo":"BabylonJS/Babylon.js","slug":"deflate-invalid-code-length-repeat","errorCode":null,"errorMessage":"deflate: invalid code length repeat","messagePattern":"deflate: invalid code length repeat","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/dev/loaders/src/FBX/parsers/zlibInflate.ts","lineNumber":378,"sourceCode":"        distanceTree: new HuffmanTree(distanceLengths, { allowEmpty: true }),\r\n    };\r\n}\r\n\r\nfunction readCodeLengths(reader: BitReader, codeLengthTree: HuffmanTree, count: number): number[] {\r\n    const lengths: number[] = [];\r\n    while (lengths.length < count) {\r\n        const symbol = codeLengthTree.decode(reader);\r\n        if (symbol <= 15) {\r\n            lengths.push(symbol);\r\n            continue;\r\n        }\r\n\r\n        let repeatLength: number;\r\n        let repeatedValue: number;\r\n        switch (symbol) {\r\n            case 16:\r\n                if (lengths.length === 0) {\r\n                    throw new Error(\"deflate: invalid code length repeat\");\r\n                }\r\n                repeatedValue = lengths[lengths.length - 1];\r\n                repeatLength = reader.readBits(2) + 3;\r\n                break;\r\n            case 17:\r\n                repeatedValue = 0;\r\n                repeatLength = reader.readBits(3) + 3;\r\n                break;\r\n            case 18:\r\n                repeatedValue = 0;\r\n                repeatLength = reader.readBits(7) + 11;\r\n                break;\r\n            default:\r\n                throw new Error(\"deflate: invalid code length symbol\");\r\n        }\r\n\r\n        if (lengths.length + repeatLength > count) {\r\n            throw new Error(\"deflate: invalid code length repeat\");\r","sourceCodeStart":360,"sourceCodeEnd":396,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/loaders/src/FBX/parsers/zlibInflate.ts#L360-L396","documentation":"Code-length symbol 16 repeats the previous code length 3-6 times, which is illegal when there is no previous length (it would be the very first decoded entry). This throw means the dynamic block's code-length sequence starts (or resumes) with a repeat-before-any-value pattern, indicating corrupt bitstream or misaligned reading.","triggerScenarios":"readCodeLengths (called from readDynamicTrees during inflateZlib) decodes symbol 16 from the codeLengthTree when lengths.length === 0 — no prior code length exists to repeat.","commonSituations":"Corrupt FBX files; payload sliced from the wrong offset so the code-length tree is decoded from garbage; truncated downloads losing part of the dynamic header.","solutions":["Test the extracted payload with python zlib.decompress — if it fails there too, the file is corrupt and must be re-exported/re-downloaded","Verify the property-header parsing produces the correct payload offset (payload must begin at the zlib header, immediately after the array length/encoding fields)","Check the file's byte size against the exporter's output to detect truncation","Compare behavior against the same asset exported without compression (FBX Encoding=0) to isolate the inflater"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Verify payload is well-formed zlib before custom inflation\nconst ref = decompressSync(payload); // throws if corrupt\nif (ref.byteLength !== expectedLength) throw new Error(\"Decoded size mismatch\");","typeGuard":null,"tryCatchPattern":"try {\n  const out = inflateZlib(payload, expectedLength);\n} catch (e) {\n  if (e instanceof Error && e.message === \"deflate: invalid code length repeat\") {\n    throw new Error(\"FBX compressed array corrupt (bad code-length repeat)\", { cause: e });\n  }\n  throw e;\n}","preventionTips":["Validate offsets: payload must start at the zlib CMF byte, not inside the property header","Re-download files whose size differs from the source manifest","Run one golden FBX file through the loader in CI to catch parser regressions"],"tags":["deflate","dynamic-huffman","corrupt-data","fbx"],"backgroundTag":"invalid-huffman-table","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}