{"record":{"id":"9d40b5960fb717f9","repo":"can1357/oh-my-pi","slug":"invalid-xz-stream-compressed-block-size-is-invali","errorCode":null,"errorMessage":"Invalid XZ stream: compressed block size is invalid","messagePattern":"Invalid XZ stream: compressed block size is invalid","errorType":"exception","errorClass":"ArchiveError","httpStatus":null,"severity":"error","filePath":"packages/utils/src/ar/codecs/xz.ts","lineNumber":456,"sourceCode":"\tif ((flags & 0x3c) !== 0) throw new ArchiveError(\"Unsupported XZ block flags\");\n\tconst filterCount = (flags & 3) + 1;\n\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)","sourceCodeStart":438,"sourceCodeEnd":474,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/utils/src/ar/codecs/xz.ts#L438-L474","documentation":"The block's compressed size is derived from the stream index record: unpaddedSize minus header size minus integrity-check size. If that arithmetic is not a safe positive integer, the index record contradicts the header and the stream is invalid. The decoder throws instead of reading a bogus span.","triggerScenarios":"xzDecompress processes a block whose index record's unpaddedSize is smaller than headerSize + integritySize (or yields a non-integer), so compressedSize computes to <= 0 or non-safe-integer.","commonSituations":"Corrupted stream indexes (bit rot), archives assembled by splicing blocks from different streams, or truncated files whose index was reconstructed incorrectly.","solutions":["Verify the archive with `xz -t`; a corrupted index usually means the whole file is unusable — re-download it.","Do not splice or concatenate XZ streams/blocks manually; keep whole streams intact.","If you control the producer, ensure the stream index accurately records unpadded/uncompressed sizes per block.","Wrap decompression in error handling so corrupt archives surface as clean failures rather than crashes."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if (!isXz(bytes)) throw new Error('Not an XZ stream');\nif (bytes.byteLength === 0) throw new Error('Empty archive buffer');","typeGuard":null,"tryCatchPattern":"try {\n\treturn await xzDecompress(bytes, maxOutput);\n} catch (err) {\n\tif (err instanceof ArchiveError) {\n\t\tlogger.error('XZ index/header inconsistent', { message: err.message });\n\t\treturn null;\n\t}\n\tthrow err;\n}","preventionTips":["Keep XZ streams whole — never splice blocks or indexes between files.","Validate archives at ingest time and fail fast on corruption.","Watch storage health: repeated index corruption suggests disk issues.","Use the standard xz tool to produce archives consumed by this decoder."],"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"}