{"record":{"id":"ac86d661213933da","repo":"can1357/oh-my-pi","slug":"invalid-xz-stream-block-compressed-size-mismatch","errorCode":null,"errorMessage":"Invalid XZ stream: block compressed size mismatch","messagePattern":"Invalid XZ stream: block compressed size mismatch","errorType":"exception","errorClass":"ArchiveError","httpStatus":null,"severity":"error","filePath":"packages/utils/src/ar/codecs/xz.ts","lineNumber":458,"sourceCode":"\tconst declaredCompressed = (flags & 0x40) !== 0 ? readVarInt(cursor) : undefined;\n\tconst declaredUncompressed = (flags & 0x80) !== 0 ? readVarInt(cursor) : undefined;\n\tconst filters: XzFilter[] = [];\n\tfor (let index = 0; index < filterCount; index++) {\n\t\tconst id = readVarInt(cursor);\n\t\tconst propertySize = readVarInt(cursor);\n\t\tif (propertySize > cursor.limit - cursor.pos)\n\t\t\tthrow new ArchiveError(\"Invalid XZ stream: truncated filter properties\");\n\t\tfilters.push({ id, properties: bytes.slice(cursor.pos, cursor.pos + propertySize) });\n\t\tcursor.pos += propertySize;\n\t}\n\twhile (cursor.pos < cursor.limit)\n\t\tif (bytes[cursor.pos++] !== 0) throw new ArchiveError(\"Invalid XZ stream: non-zero block header padding\");\n\tconst integritySize = checkSize(checkId);\n\tconst compressedSize = record.unpaddedSize - headerSize - integritySize;\n\tif (!Number.isSafeInteger(compressedSize) || compressedSize <= 0)\n\t\tthrow new ArchiveError(\"Invalid XZ stream: compressed block size is invalid\");\n\tif (declaredCompressed !== undefined && declaredCompressed !== compressedSize)\n\t\tthrow new ArchiveError(\"Invalid XZ stream: block compressed size mismatch\");\n\tif (declaredUncompressed !== undefined && declaredUncompressed !== record.uncompressedSize)\n\t\tthrow new ArchiveError(\"Invalid XZ stream: block uncompressed size mismatch\");\n\tconst compressedStart = offset + headerSize;\n\tconst compressedEnd = compressedStart + compressedSize;\n\tconst paddingSize = (4 - ((headerSize + compressedSize) & 3)) & 3;\n\tconst checkStart = compressedEnd + paddingSize;\n\tif (checkStart + integritySize > bytes.byteLength) throw new ArchiveError(\"Invalid XZ stream: truncated block data\");\n\tconst last = filters[filters.length - 1]!;\n\tif (last.id !== 0x21 || last.properties.byteLength !== 1)\n\t\tthrow new ArchiveError(`Unsupported XZ terminal filter ID 0x${last.id.toString(16)} (LZMA2 required)`);\n\tlet output = await lzma2Decompress(\n\t\tlast.properties[0]!,\n\t\tbytes.subarray(compressedStart, compressedEnd),\n\t\trecord.uncompressedSize,\n\t);\n\tif (output.byteLength !== record.uncompressedSize)\n\t\tthrow new ArchiveError(\"Invalid XZ stream: decoded block size mismatch\");\n\toutput = output.slice();","sourceCodeStart":440,"sourceCodeEnd":476,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/utils/src/ar/codecs/xz.ts#L440-L476","documentation":"The block header optionally declares the compressed size (flags bit 0x40). When present, it must equal the size computed from the index record (unpaddedSize - headerSize - integritySize). A mismatch means the index and header disagree, so the stream is corrupt or non-conforming.","triggerScenarios":"xzDecompress reads a block header with the compressed-size-present flag set whose declared value differs from record.unpaddedSize - headerSize - integritySize.","commonSituations":"Archives damaged in transit or on disk, files produced by encoders that write inconsistent index records, or streams reassembled from fragments.","solutions":["Re-acquire a known-good copy of the archive and verify with `xz -t`.","If the file was produced by your own tooling, fix the encoder to keep the header-declared sizes and index records consistent.","Check for accidental byte-level modification of the archive (editors, transfer encodings, text-mode copies).","Catch ArchiveError around xzDecompress and report the specific file as invalid."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const ok = await $`xz -t ${path}`.quiet().nothrow();\nif (ok.exitCode !== 0) throw new Error(`XZ archive failed integrity test: ${path}`);","typeGuard":null,"tryCatchPattern":"try {\n\treturn await xzDecompress(bytes, maxOutput);\n} catch (err) {\n\tif (err instanceof ArchiveError && /mismatch/i.test(err.message)) {\n\t\tthrow new Error('Archive metadata inconsistent — file is corrupt, re-obtain it');\n\t}\n\tthrow err;\n}","preventionTips":["Run `xz -t` (or an equivalent integrity check) before programmatic decompression.","Compare archive size/checksum against the source of truth after transfer.","Avoid text-mode or encoding-translating copies of binary archives.","Regenerate archives with a current, standard xz version."],"tags":["archive","xz","corrupt-data","index-mismatch"],"backgroundTag":"xz-stream-corrupt","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}