{"record":{"id":"a288db5b351be5bd","repo":"MuntashirAkon/AppManager","slug":"bzip2-block-size-is-invalid","errorCode":null,"errorMessage":"BZip2 block size is invalid","messagePattern":"BZip2 block size is invalid","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java","lineNumber":247,"sourceCode":"            bin.clearBitCache();\n        }\n\n        final int magic0 = readNextByte(this.bin);\n        if (magic0 == -1 && !isFirstStream) {\n            return false;\n        }\n        final int magic1 = readNextByte(this.bin);\n        final int magic2 = readNextByte(this.bin);\n\n        if (magic0 != 'B' || magic1 != 'Z' || magic2 != 'h') {\n            throw new IOException(isFirstStream\n                    ? \"Stream is not in the BZip2 format\"\n                    : \"Garbage after a valid BZip2 stream\");\n        }\n\n        final int blockSize = readNextByte(this.bin);\n        if ((blockSize < '1') || (blockSize > '9')) {\n            throw new IOException(\"BZip2 block size is invalid\");\n        }\n\n        this.blockSize100k = blockSize - '0';\n\n        this.computedCombinedCRC = 0;\n\n        return true;\n    }\n\n    private void initBlock() throws IOException {\n        final BitInputStream bin = this.bin;\n        char magic0;\n        char magic1;\n        char magic2;\n        char magic3;\n        char magic4;\n        char magic5;\n","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java#L229-L265","documentation":"After the 'BZh' magic, the fourth byte must be '1'..'9' specifying the 100k block size used at compression time. init() throws IOException(\"BZip2 block size is invalid\") when this byte is out of range, so the stream header is malformed or not bzip2 at all.","triggerScenarios":"A stream whose 4th byte is not an ASCII digit 1-9 — truncated/corrupt headers, files starting with 'BZh' by coincidence, or corrupted first block.","commonSituations":"Partially downloaded .bz2 files; binary files that happen to start with 'BZh'; man-in-the-middle or transfer corruption; hand-crafted or fuzzed inputs.","solutions":["Validate the input file integrity (checksum the download, re-transfer).","Confirm the file is genuine bzip2 output with `file` or `bzip2 -t`.","Catch IOException from the BZip2CompressorInputStream constructor and fall back to a detected format."],"exampleFix":"// before\nInputStream in = new BZip2CompressorInputStream(new FileInputStream(maybeBz2));\n// after\ntry {\n    InputStream in = new BZip2CompressorInputStream(new FileInputStream(maybeBz2));\n} catch (IOException e) {\n    throw new IOException(\"Corrupt or non-bzip2 input: \" + e.getMessage(), e);\n}","handlingStrategy":"validation","validationCode":"PushbackInputStream pb = new PushbackInputStream(src, 4);\nbyte[] h = new byte[4];\nif (pb.read(h) != 4 || h[0] != 'B' || h[1] != 'Z' || h[2] != 'h'\n        || h[3] < '1' || h[3] > '9') {\n    pb.unread(h);\n    throw new IllegalArgumentException(\"malformed bzip2 header\");\n}\npb.unread(h);","typeGuard":"static boolean hasValidBlockSize(byte[] head) {\n    return head.length >= 4 && head[3] >= '1' && head[3] <= '9';\n}","tryCatchPattern":"try {\n    InputStream in = new BZip2CompressorInputStream(pb);\n} catch (IOException e) {\n    if (String.valueOf(e.getMessage()).contains(\"block size is invalid\")) {\n        throw new IOException(\"corrupt bzip2 header\", e);\n    }\n    throw e;\n}","preventionTips":["Checksum downloads before decompressing.","Treat any header-parse failure as data corruption, not retryable.","Source .bz2 files only from trusted, verified transfers."],"tags":["io","corrupt-data","format-validation"],"backgroundTag":"invalid-stream-format","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}