{"record":{"id":"5b54395f509c9f87","repo":"HMCL-dev/HMCL","slug":"valid-png-colour-types-are-0-2-3-4-6-type-d","errorCode":null,"errorMessage":"Valid PNG colour types are 0, 2, 3, 4, 6. Type '%d' is invalid","messagePattern":"Valid PNG colour types are 0, 2, 3, 4, 6\\. Type '(.+?)' is invalid","errorType":"exception","errorClass":"PngIntegrityException","httpStatus":null,"severity":"error","filePath":"HMCL/src/main/java/org/jackhuang/hmcl/ui/image/apng/PngColourType.java","lineNumber":58,"sourceCode":"\n    public boolean supportsSubByteDepth() {\n        return code == 0 || code == 3;\n    }\n\n    public static PngColourType fromByte(byte b) throws PngException {\n        switch (b) {\n            case 0:\n                return PNG_GREYSCALE;\n            case 2:\n                return PNG_TRUECOLOUR;\n            case 3:\n                return PNG_INDEXED_COLOUR;\n            case 4:\n                return PNG_GREYSCALE_WITH_ALPHA;\n            case 6:\n                return PNG_TRUECOLOUR_WITH_ALPHA;\n            default:\n                throw new PngIntegrityException(String.format(\"Valid PNG colour types are 0, 2, 3, 4, 6. Type '%d' is invalid\", b));\n        }\n    }\n}\n","sourceCodeStart":40,"sourceCodeEnd":62,"githubUrl":"https://github.com/HMCL-dev/HMCL/blob/24702dc5a0214034f4c27166d5fd30cad08cec19/HMCL/src/main/java/org/jackhuang/hmcl/ui/image/apng/PngColourType.java#L40-L62","documentation":"Thrown by PngColourType.fromByte as PngIntegrityException when the IHDR colour type byte is not one of the valid PNG colour types 0 (greyscale), 2 (truecolour), 3 (indexed), 4 (greyscale+alpha), 6 (truecolour+alpha). Values 1, 5, 7+ are invalid per the PNG spec.","triggerScenarios":"Parsing a PNG whose IHDR chunk contains colour type 1, 5, or >= 7 — i.e. a corrupt header, a truncated/mangled file, or non-PNG data misidentified as a PNG.","commonSituations":"Corrupted downloads or disk sectors; files with overwritten headers; crafted PNGs in security testing; text files renamed to .png.","solutions":["Re-download or re-export the PNG file — the header is corrupt","Verify the file starts with a valid PNG signature and IHDR using pngcheck","Catch PngIntegrityException during parse and reject the file with a clear message","If processing untrusted input, validate signature + IHDR fields before full decode"],"exampleFix":"// before\nPngColourType ct = PngColourType.fromByte(ihdrColourType); // throws on 1/5/7\n// after\nif (ihdrColourType == 0 || ihdrColourType == 2 || ihdrColourType == 3\n        || ihdrColourType == 4 || ihdrColourType == 6) {\n    ct = PngColourType.fromByte(ihdrColourType);\n} else {\n    rejectFile(\"Unsupported colour type \" + ihdrColourType);\n}","handlingStrategy":"validation","validationCode":"int ct = readIhdrColourType(bytes);\nif (ct != 0 && ct != 2 && ct != 3 && ct != 4 && ct != 6) {\n    throw new IllegalArgumentException(\"Invalid PNG colour type: \" + ct);\n}","typeGuard":"static boolean isKnownColourType(byte b) {\n    int v = b & 0xFF;\n    return v == 0 || v == 2 || v == 3 || v == 4 || v == 6;\n}","tryCatchPattern":"try {\n    colourType = PngColourType.fromByte(b);\n} catch (PngIntegrityException e) {\n    throw new IOException(\"Corrupt PNG header: \" + e.getMessage(), e);\n}","preventionTips":["Validate the 8-byte PNG signature before parsing IHDR","Run pngcheck on suspicious files","Reject truncated or hand-edited PNGs early","Surface a 'file is corrupt' message rather than a raw decode stack"],"tags":["png","ihdr","validation"],"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-16T04:17:20.429Z"}