{"record":{"id":"7854ca546e4954aa","repo":"HMCL-dev/HMCL","slug":"received-trns-data-length-is-invalid-should-be-1","errorCode":null,"errorMessage":"Received tRNS data length is invalid. Should be >1 && < %d but is %d","messagePattern":"Received tRNS data length is invalid\\. Should be >1 && < (.+?) but is (.+?)","errorType":"exception","errorClass":"PngIntegrityException","httpStatus":null,"severity":"error","filePath":"HMCL/src/main/java/org/jackhuang/hmcl/ui/image/apng/argb8888/BasicArgb8888Director.java","lineNumber":27,"sourceCode":"/**\n * Common functionality for Argb8888Director implementations.\n */\npublic abstract class BasicArgb8888Director<ResultT> implements Argb8888Director<ResultT> {\n    protected Argb8888ScanlineProcessor scanlineProcessor;\n\n    @Override\n    public void receivePalette(Argb8888Palette palette) {\n        scanlineProcessor.setPalette(palette);\n    }\n\n    @Override\n    public void processTransparentPalette(byte[] bytes, int position, int length) throws PngException {\n        Argb8888Palette palette = scanlineProcessor.getPalette();\n        if (null == palette) {\n            throw new PngIntegrityException(\"Received tRNS data but no palette is in place\");\n        }\n        if (length <= 0 || length > palette.size()) {\n            throw new PngIntegrityException(String.format(\"Received tRNS data length is invalid. Should be >1 && < %d but is %d\", palette.size(), length));\n        }\n        for (int i = 0; i < length; i++) {\n            final int alpha = 0xff & bytes[position + i];\n            palette.argbArray()[i] = alpha << 24 | palette.argbArray()[i] & 0x00FFFFFF;\n        }\n    }\n\n    @Override\n    public void processTransparentGreyscale(byte k1, byte k0) throws PngException {\n        scanlineProcessor.processTransparentGreyscale(k1, k0);\n    }\n\n    @Override\n    public void processTransparentTruecolour(byte r1, byte r0, byte g1, byte g0, byte b1, byte b0) throws PngException {\n        scanlineProcessor.processTransparentTruecolour(r1, r0, g1, g0, b1, b0);\n    }\n\n}","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/HMCL-dev/HMCL/blob/24702dc5a0214034f4c27166d5fd30cad08cec19/HMCL/src/main/java/org/jackhuang/hmcl/ui/image/apng/argb8888/BasicArgb8888Director.java#L9-L45","documentation":"Thrown by processTransparentPalette when a tRNS chunk's data length is zero or exceeds the number of entries in the established PLTE palette. The library validates that transparency alpha values map onto existing palette indices; a mismatched length means the chunk is malformed or out of order.","triggerScenarios":"A tRNS chunk arrives with length 0, or with more bytes than the palette size (palette.size()), or a tRNS is delivered without any palette having been processed (that raises a different message, but wrong order can lead to stale palette sizes).","commonSituations":"Hand-edited or corrupted PNG files, PNG steganography test images, chunk streams where tRNS precedes PLTE or was truncated by a partial download.","solutions":["Verify the tRNS chunk data length in the PNG file does not exceed the PLTE entry count (length must be 1..paletteSize).","Ensure the PLTE chunk is parsed before tRNS so the palette is in place and sized correctly.","Re-export or re-save the PNG with a compliant encoder (e.g. ImageMagick, pngcrush) to regenerate a valid tRNS chunk.","Catch PngIntegrityException around decode and treat the file as invalid input."],"exampleFix":"// before: trusting chunk length\nreadTransparentPalette(bytes, pos, chunkLength);\n// after: clamp/validate against palette\nint safe = Math.min(chunkLength, palette.size());\nif (safe <= 0) throw new PngIntegrityException(\"empty tRNS\");\nreadTransparentPalette(bytes, pos, safe);","handlingStrategy":"try-catch","validationCode":"// parse chunk table first\nif (trnsLength < 1 || trnsLength > plteEntryCount) throw new IllegalArgumentException(\"bad tRNS length\");","typeGuard":null,"tryCatchPattern":"try { director.processTransparentPalette(bytes, pos, len); } catch (PngIntegrityException e) { log.warn(\"Invalid tRNS: \" + e.getMessage()); fallbackToOpaqueRendering(); }","preventionTips":["Validate tRNS length against PLTE entry count before decoding.","Ensure PLTE is processed before tRNS.","Re-encode suspect images with a compliant PNG tool.","Add pngcheck to ingestion pipelines to pre-screen files."],"tags":["png","image-decoding","validation","apng"],"backgroundTag":"value-out-of-range","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"}