{"record":{"id":"2c41220eb05ba16b","repo":"HMCL-dev/HMCL","slug":"invalid-bit-depth","errorCode":null,"errorMessage":"Invalid bit depth ","messagePattern":"Invalid bit depth ","errorType":"exception","errorClass":"PngIntegrityException","httpStatus":null,"severity":"error","filePath":"HMCL/src/main/java/org/jackhuang/hmcl/ui/image/apng/chunks/PngHeader.java","lineNumber":192,"sourceCode":"    public PngHeader adjustFor(PngFrameControl frame) {\n        if (frame == null) {\n            return this;\n        } else {\n            return new PngHeader(frame.width, frame.height, this.bitDepth, this.colourType, this.compressionMethod, this.filterMethod, this.interlaceMethod);\n        }\n    }\n\n    public static void checkHeaderParameters(int width, int height, byte bitDepth, PngColourType colourType, byte compressionMethod, byte filterMethod, byte interlaceMethod) throws PngException {\n\n        switch (bitDepth) {\n            case 1:\n            case 2:\n            case 4:\n            case 8:\n            case 16:\n                break; // all fine\n            default:\n                throw new PngIntegrityException(\"Invalid bit depth \" + bitDepth);\n        }\n\n        // thanks to pypng\n        if (colourType.isIndexed() && bitDepth > 8) {\n            throw new PngIntegrityException(String.format(\n                    \"Indexed images (colour type %d) cannot have bitdepth > 8 (bit depth %d).\" +\n                            \" See http://www.w3.org/TR/2003/REC-PNG-20031110/#table111 .\", colourType.code, bitDepth));\n        }\n\n        if (bitDepth < 8 && !colourType.supportsSubByteDepth()) {\n            throw new PngIntegrityException(String.format(\n                    \"Illegal combination of bit depth (%d) and colour type (%d).\" +\n                            \" See http://www.w3.org/TR/2003/REC-PNG-20031110/#table111 .\", colourType.code, bitDepth));\n        }\n\n        if (interlaceMethod != 0) {\n            throw new PngFeatureException(\"Interlaced images are not yet supported\");\n        }","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/HMCL-dev/HMCL/blob/24702dc5a0214034f4c27166d5fd30cad08cec19/HMCL/src/main/java/org/jackhuang/hmcl/ui/image/apng/chunks/PngHeader.java#L174-L210","documentation":"This integrity guard in PngHeader.checkHeaderParameters fires when a PNG IHDR chunk declares a bit depth value outside the PNG specification's legal set (1, 2, 4, 8, 16). The input at fault is the raw bitDepth byte parsed from the IHDR chunk; any other value means the file is malformed or corrupt, so parsing cannot proceed safely.","triggerScenarios":"Parsing a PNG whose IHDR byte 8 (bit depth) holds a value like 0, 3, 5, 32, or random garbage because the file is not actually a PNG or has been corrupted.","commonSituations":"Feeding renamed non-PNG files (e.g. a JPEG renamed to .png), truncated/corrupted downloads, fuzzed or malicious images.","solutions":["Check the file starts with the 8-byte PNG signature and a well-formed IHDR before decoding.","Re-obtain or re-encode the image; the source file's header is corrupt.","Catch PngIntegrityException and fall back to another decoder or show an 'unsupported/corrupt image' message."],"exampleFix":"// before\nPngHeader.from(dataInput);\n// after: sniff first\nbyte[] sig = new byte[8];\nin.readFully(sig);\nif (!Arrays.equals(sig, PNG_SIGNATURE)) throw new PngIntegrityException(\"not a PNG\");\nPngHeader.from(dataInput);","handlingStrategy":"validation","validationCode":"// after reading IHDR fields\nif (bitDepth != 1 && bitDepth != 2 && bitDepth != 4 && bitDepth != 8 && bitDepth != 16) {\n    throw new IllegalArgumentException(\"illegal PNG bit depth \" + bitDepth);\n}","typeGuard":null,"tryCatchPattern":"try { header = PngHeader.from(dis); } catch (PngIntegrityException e) { reportCorruptImage(file, e); return null; }","preventionTips":["Verify the PNG signature before parsing.","Pre-screen files with pngcheck.","Never trust file extensions; sniff content.","Handle truncated downloads before decode."],"tags":["png","image-decoding","validation","header"],"backgroundTag":"invalid-enum-value","analyzedSha":"24702dc5a0214034f4c27166d5fd30cad08cec19","analyzedAt":"2026-09-10T12:36:46.680Z","contentChangedAt":"2026-09-10T12:36:46.680Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}