{"record":{"id":"51ef8f6536ac2bf0","repo":"SixLabors/ImageSharp","slug":"invalid-compressed-png-metadata","errorCode":null,"errorMessage":"Invalid compressed PNG metadata.","messagePattern":"Invalid compressed PNG metadata\\.","errorType":"exception","errorClass":"InvalidImageContentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Png/PngDecoderCore.cs","lineNumber":2011,"sourceCode":"                    {\n                        uncompressedBytesArray = [];\n                        return false;\n                    }\n\n                    memoryStreamOutput.Write(destUncompressedData[..bytesRead]);\n                    bytesRead = inflateStream.CompressedStream.Read(destUncompressedData);\n                }\n\n                uncompressedBytesArray = memoryStreamOutput.ToArray();\n                return true;\n            }\n            catch (InvalidDataException ex)\n            {\n                // ICC and text chunks are already bounded in memory, so rejecting their compressed contents\n                // does not lose the next chunk boundary. Apply the ancillary policy without keeping partial output.\n                if (this.Options.SegmentIntegrityHandling == SegmentIntegrityHandling.Strict)\n                {\n                    throw new InvalidImageContentException(\"Invalid compressed PNG metadata.\", ex);\n                }\n\n                uncompressedBytesArray = [];\n                return false;\n            }\n        }\n    }\n\n    /// <summary>\n    /// Compares two ReadOnlySpan&lt;char&gt;s in a case-insensitive method.\n    /// This is only needed because older frameworks are missing the extension method.\n    /// </summary>\n    /// <param name=\"span1\">The first <see cref=\"Span{T}\"/> to compare.</param>\n    /// <param name=\"span2\">The second <see cref=\"Span{T}\"/> to compare.</param>\n    /// <returns>True if the spans were identical, false otherwise.</returns>\n    private static bool StringEqualsInsensitive(ReadOnlySpan<char> span1, ReadOnlySpan<char> span2)\n        => span1.Equals(span2, StringComparison.OrdinalIgnoreCase);\n","sourceCodeStart":1993,"sourceCodeEnd":2029,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Png/PngDecoderCore.cs#L1993-L2029","documentation":"PngDecoderCore throws this InvalidImageContentException when a compressed ancillary PNG chunk (e.g. iCCP, tEXt/zTXt/iTXt) fails zlib decompression and SegmentIntegrityHandling is Strict. The strict policy treats undecompressable metadata as fatal instead of dropping the segment; in non-strict modes the corrupt chunk's content is discarded (uncompressedBytesArray = []) and decoding continues.","triggerScenarios":"Decoding a PNG with a corrupt or unsupported compressed metadata chunk while PngDecoderOptions.SegmentIntegrityHandling == SegmentIntegrityHandling.Strict; e.g. a truncated zTXt chunk or invalid zlib stream in iCCP.","commonSituations":"Files truncated mid-chunk, metadata written by tools using non-standard deflate data, deliberately malformed inputs in security-sensitive pipelines, or toggling Strict handling on untrusted image sets.","solutions":["Set SegmentIntegrityHandling to a non-strict value (e.g. Ignore/Omit) if losing ancillary metadata (ICC profile, text) is acceptable.","Remove or repair the corrupt metadata chunk in the source file (e.g. strip iCCP with a PNG utility).","Re-export the image so metadata chunks are rewritten with valid zlib streams.","Wrap decode in try-catch and fall back to a relaxed-options decode on failure."],"exampleFix":"// before\nvar options = new DecoderOptions { SegmentIntegrityHandling = SegmentIntegrityHandling.Strict };\nusing var image = Image.Load(options, \"meta.png\");\n// after\ntry\n{\n    var strict = new DecoderOptions { SegmentIntegrityHandling = SegmentIntegrityHandling.Strict };\n    using var image = Image.Load(strict, \"meta.png\");\n}\ncatch (InvalidImageContentException)\n{\n    var relaxed = new DecoderOptions { SegmentIntegrityHandling = SegmentIntegrityHandling.Omit };\n    using var image = Image.Load(relaxed, \"meta.png\"); // skip corrupt metadata chunk\n}","handlingStrategy":"try-catch","validationCode":"// decode strict first, fall back to relaxed handling when metadata is corrupt\nvar strict = new DecoderOptions { SegmentIntegrityHandling = SegmentIntegrityHandling.Strict };","typeGuard":null,"tryCatchPattern":"try\n{\n    using var image = Image.Load(strictOptions, path);\n}\ncatch (InvalidImageContentException ex) when (ex.Message == \"Invalid compressed PNG metadata.\")\n{\n    var relaxed = new DecoderOptions { SegmentIntegrityHandling = SegmentIntegrityHandling.Omit };\n    using var image = Image.Load(relaxed, path); // drops the corrupt iCCP/text chunk\n}","preventionTips":["Only enable SegmentIntegrityHandling.Strict when corrupt metadata must abort processing (security pipelines).","Strip iCCP/zTXt chunks from untrusted files before strict decoding.","Prefer re-exporting files whose ancillary chunks fail decompression."],"tags":["png","zlib","metadata","strict-mode"],"backgroundTag":"invalid-config-value","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}