{"record":{"id":"d9d390048f7f4f8e","repo":"MuntashirAkon/AppManager","slug":"corrupted-input-nselectors-value-negative","errorCode":null,"errorMessage":"Corrupted input, nSelectors value negative","messagePattern":"Corrupted input, nSelectors value negative","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java","lineNumber":478,"sourceCode":"        Arrays.fill(inUse, false);\n        for (int i = 0; i < 16; i++) {\n            if ((inUse16 & (1 << i)) != 0) {\n                final int i16 = i << 4;\n                for (int j = 0; j < 16; j++) {\n                    if (bsGetBit(bin)) {\n                        inUse[i16 + j] = true;\n                    }\n                }\n            }\n        }\n\n        makeMaps();\n        final int alphaSize = this.nInUse + 2;\n        /* Now the selectors */\n        final int nGroups = bsR(bin, 3);\n        final int selectors = bsR(bin, 15);\n        if (selectors < 0) {\n            throw new IOException(\"Corrupted input, nSelectors value negative\");\n        }\n        checkBounds(alphaSize, MAX_ALPHA_SIZE + 1, \"alphaSize\");\n        checkBounds(nGroups, N_GROUPS + 1, \"nGroups\");\n\n        // Don't fail on nSelectors overflowing boundaries but discard the values in overflow\n        // See https://gnu.wildebeest.org/blog/mjw/2019/08/02/bzip2-and-the-cve-that-wasnt/\n        // and https://sourceware.org/ml/bzip2-devel/2019-q3/msg00007.html\n\n        for (int i = 0; i < selectors; i++) {\n            int j = 0;\n            while (bsGetBit(bin)) {\n                j++;\n            }\n            if (i < MAX_SELECTORS) {\n                selectorMtf[i] = (byte) j;\n            }\n        }\n        final int nSelectors = selectors > MAX_SELECTORS ? MAX_SELECTORS : selectors;","sourceCodeStart":460,"sourceCodeEnd":496,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java#L460-L496","documentation":"recvDecodingTables reads the 15-bit nSelectors count from the block header. Valid bzip2 data always has a non-negative nSelectors; a negative value means the bitstream is corrupt, so the decompressor aborts with this explicit message instead of allocating absurd memory or mis-decoding.","triggerScenarios":"getAndMoveToFrontDecode -> recvDecodingTables encounters a corrupted block header while BZip2CompressorInputStream is reading a block; the 15-bit selector count decodes negative.","commonSituations":"Truncated or corrupted .bz2 files, non-bzip2 bytes fed into the stream, transfer corruption, security fuzzing inputs.","solutions":["Validate the source archive (`bzip2 -t`) and get a clean copy.","Confirm the InputStream position: don't start decompression mid-file or after full consumption.","Catch IOException around read loops and surface a user-facing 'corrupt archive' error.","Check storage/network for bit-rot if corruption recurs on many files."],"exampleFix":"// before\nwhile ((n = bz.read(buf)) >= 0) out.write(buf, 0, n); // raw IOException escapes\n// after\ntry {\n    while ((n = bz.read(buf)) >= 0) out.write(buf, 0, n);\n} catch (IOException e) {\n    throw new CorruptArchiveException(\"bad bzip2 data: \" + e.getMessage(), e);\n}","handlingStrategy":"try-catch","validationCode":"Process p = new ProcessBuilder(\"bzip2\", \"-t\", path.toString()).redirectErrorStream(true).start();\nif (p.waitFor() != 0) throw new IOException(\"archive failed integrity check: \" + path);","typeGuard":null,"tryCatchPattern":"try (BZip2CompressorInputStream bz = new BZip2CompressorInputStream(in)) {\n    IOUtils.copy(bz, out);\n} catch (IOException e) {\n    throw new CorruptArchiveException(\"nSelectors/block-header corruption: \" + e.getMessage(), e);\n}","preventionTips":["Run integrity checks on archives at ingestion time","Do not resume or seek within bzip2 streams mid-block","Watch for repeated corruption — check storage/network health","Log the offending file path with the exception"],"tags":["io","bzip2","corrupt-input","block-header"],"backgroundTag":"corrupt-input-data","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}