{"record":{"id":"f9abbd45ccfae98f","repo":"SixLabors/ImageSharp","slug":"invalid-webp-data-could-not-read-chunk-type","errorCode":null,"errorMessage":"Invalid Webp data, could not read chunk type.","messagePattern":"Invalid Webp data, could not read chunk type\\.","errorType":"exception","errorClass":"ImageFormatException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Webp/WebpChunkParsingUtils.cs","lineNumber":364,"sourceCode":"\n    /// <summary>\n    /// Identifies the chunk type from the chunk.\n    /// </summary>\n    /// <param name=\"stream\">The stream to read the data from.</param>\n    /// <param name=\"buffer\">Buffer to store the data read from the stream.</param>\n    /// <exception cref=\"ImageFormatException\">\n    /// Thrown if the input stream is not valid.\n    /// </exception>\n    public static WebpChunkType ReadChunkType(BufferedReadStream stream, Span<byte> buffer)\n    {\n        if (stream.Read(buffer) == 4)\n        {\n            return (WebpChunkType)BinaryPrimitives.ReadUInt32BigEndian(buffer);\n        }\n\n        // While we ignore unknown chunks we still need a to be a ble to read a chunk type\n        // known or otherwise from the stream.\n        throw new ImageFormatException(\"Invalid Webp data, could not read chunk type.\");\n    }\n\n    /// <summary>\n    /// Reads the ICCP chunk from the stream.\n    /// </summary>\n    /// <param name=\"stream\">The stream to decode from.</param>\n    /// <param name=\"metadata\">The image metadata.</param>\n    /// <param name=\"ignoreMetadata\">If true, metadata will be ignored.</param>\n    /// <param name=\"executeAncillarySegmentAction\">Executes profile parsing under the decoder's integrity policy.</param>\n    public static void ReadIccProfile(\n        BufferedReadStream stream,\n        ImageMetadata metadata,\n        bool ignoreMetadata,\n        Action<Action> executeAncillarySegmentAction)\n    {\n        ulong chunkSize = ReadPaddedChunkSize(stream, stackalloc byte[4], true);\n\n        // ICCP precedes image/frame data. Its framing must be readable even when","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Webp/WebpChunkParsingUtils.cs#L346-L382","documentation":"WebpChunkParsingUtils.ReadChunkType throws ImageFormatException when the 4-byte chunk type (FourCC) cannot be read from the stream. Unknown chunk types are normally skipped, but a chunk type must still be readable to parse the RIFF structure; failing to read 4 bytes means the data is truncated or not WebP at that offset.","triggerScenarios":"Decoding a WebP where the stream ends inside a chunk's FourCC field; also reached when the header structure is misplaced so the reader expects a chunk type at end-of-data.","commonSituations":"Truncated downloads; files where a prior chunk size was corrupted causing the parser to seek past real data; saving a partial file during an in-progress transfer.","solutions":["Verify file integrity and re-download; check the file starts with RIFF....WEBP.","Catch ImageFormatException around decoding to classify input as corrupt.","Validate chunk structure with a tool (webpinfo) to find where the file diverges from the RIFF spec.","Ensure no other code has advanced the stream before the decoder receives it."],"exampleFix":"// before\nusing var image = Image.Load(maybeWebp);\n// after\nusing var ms = new MemoryStream();\nmaybeWebp.CopyTo(ms); ms.Position = 0;\ntry { using var image = Image.Load(ms); }\ncatch (ImageFormatException) { /* not valid/truncated WebP */ }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { using var image = Image.Load(stream); }\ncatch (ImageFormatException ex) { log.Warn(ex, \"Could not read WebP chunk type — file truncated/corrupt\"); }","preventionTips":["Re-download truncated files","Run webpinfo on suspect inputs","Reset stream position and avoid shared stream reads before decode"],"tags":["webp","riff","chunk","truncated-data"],"backgroundTag":"file-read-failed","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"}