{"record":{"id":"5a5546b4ccefc2ed","repo":"SixLabors/ImageSharp","slug":"bad-method-for-zlib-header-cmf-cmf","errorCode":null,"errorMessage":"Bad method for ZLIB header: cmf={cmf}","messagePattern":"Bad method for ZLIB header: cmf=(.+?)","errorType":"exception","errorClass":"ImageFormatException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Compression/Zlib/ZlibInflateReader.cs","lineNumber":95,"sourceCode":"        {\n            // CINFO is the base-2 logarithm of the LZ77 window size, minus eight.\n            int cinfo = (cmf & 0xF0) >> 4;\n\n            if (cinfo > 7)\n            {\n                if (isCriticalChunk)\n                {\n                    // Values of CINFO above 7 are not allowed in RFC1950.\n                    // CINFO is not defined in this specification for CM not equal to 8.\n                    throw new ImageFormatException($\"Invalid window size for ZLIB header: cinfo={cinfo}\");\n                }\n\n                return false;\n            }\n        }\n        else if (isCriticalChunk)\n        {\n            throw new ImageFormatException($\"Bad method for ZLIB header: cmf={cmf}\");\n        }\n        else\n        {\n            return false;\n        }\n\n        // The preset dictionary.\n        bool fdict = (flag & 32) != 0;\n        if (fdict)\n        {\n            // We don't need this for inflate so simply skip by the next four bytes.\n            // https://tools.ietf.org/html/rfc1950#page-6\n            InlineArray4<byte> checksumBuffer = default;\n\n            if (this.segmentStream.Read(checksumBuffer) != 4)\n            {\n                return false;\n            }","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Compression/Zlib/ZlibInflateReader.cs#L77-L113","documentation":"RFC 1950 defines CM (compression method) only as 8 (deflate). When a critical chunk's ZLIB header has a different CMF value, InitializeInflateStream throws an ImageFormatException; non-critical chunks are silently skipped. This guards against streams the inflater cannot decode.","triggerScenarios":"Decoding a PNG (or reading a ZLIB stream) whose header byte's CM field is not 8 — e.g. a header corrupted in transit or produced by a non-ZLIB wrapper written into an IDAT chunk.","commonSituations":"Corrupt or deliberately malformed PNG files; data where the zlib header was overwritten; streams extracted at a wrong offset so the header bytes don't align.","solutions":["Verify the file is a valid PNG and its data chunks are intact (check CRC and header bytes 0x78).","Re-download or re-export the image; treat the file as corrupted.","Catch ImageFormatException and skip the malformed image."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Quick sniff: first byte of the zlib stream (start of IDAT data) should be 0x78.\nbool zlibHeaderLooksValid = idatData[0] == 0x78;","typeGuard":null,"tryCatchPattern":"try { using var img = Image.Load(stream); }\ncatch (ImageFormatException ex) when (ex.Message.Contains(\"Bad method for ZLIB\"))\n{ MarkCorrupt(stream); }","preventionTips":["Only decode images from trusted sources; validate checksums on ingest.","Never hand-patch PNG chunk payloads; re-encode instead.","Log and quarantine files failing zlib header validation."],"tags":["zlib","png","corrupt-file","compression"],"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"}