{"record":{"id":"ac69ce594102be31","repo":"SixLabors/ImageSharp","slug":"invalid-webp-data-could-not-read-chunk-size","errorCode":null,"errorMessage":"Invalid Webp data, could not read chunk size.","messagePattern":"Invalid Webp data, could not read chunk size\\.","errorType":"exception","errorClass":"ImageFormatException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Webp/WebpChunkParsingUtils.cs","lineNumber":340,"sourceCode":"\n    /// <summary>\n    /// Reads a chunk's complete padded extent without wrapping a uint-sized payload length.\n    /// </summary>\n    /// <param name=\"stream\">The input stream.</param>\n    /// <param name=\"buffer\">The four-byte size buffer.</param>\n    /// <param name=\"required\">Whether an incomplete size field is an error.</param>\n    /// <returns>The padded extent, or remaining bytes when an optional size field is incomplete.</returns>\n    private static ulong ReadPaddedChunkSize(BufferedReadStream stream, Span<byte> buffer, bool required)\n    {\n        if (stream.Read(buffer) is 4)\n        {\n            uint chunkSize = BinaryPrimitives.ReadUInt32LittleEndian(buffer);\n            return (ulong)chunkSize + (chunkSize & 1);\n        }\n\n        if (required)\n        {\n            throw new ImageFormatException(\"Invalid Webp data, could not read chunk size.\");\n        }\n\n        // Return the size of the remaining data in the stream.\n        return (ulong)stream.RemainingBytes;\n    }\n\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        {","sourceCodeStart":322,"sourceCodeEnd":358,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Webp/WebpChunkParsingUtils.cs#L322-L358","documentation":"WebpChunkParsingUtils.ReadPaddedChunkSize throws ImageFormatException when it cannot read the 4-byte chunk size field and the chunk is marked required. RIFF/WebP chunk sizes are padded to even lengths; a short read here means the stream ends before the size field completes, i.e. truncated or malformed WebP data.","triggerScenarios":"Decoding/Identify on a WebP stream that ends within a chunk header where required=true; stream.Read returns fewer than 4 bytes for the size.","commonSituations":"Partially downloaded WebP files; upload truncation; seeking to a wrong offset so the reader lands near end-of-stream; corrupted storage objects.","solutions":["Validate the file is complete (byte length vs expected) and re-download if truncated.","Wrap Image.Load/Identify in try/catch (ImageFormatException) to handle corrupt input gracefully.","Ensure the stream position is at a valid chunk boundary (chunk type + size are 8-byte aligned units).","For network sources, fully buffer before decoding."],"exampleFix":"// before\nusing var image = Image.Identify(partialStream); // throws on truncated chunk\n// after\ntry { var info = Image.Identify(partialStream); }\ncatch (ImageFormatException ex) { log.Warn($\"WebP truncated/corrupt: {ex.Message}\"); }","handlingStrategy":"try-catch","validationCode":"// require a plausible minimum size and RIFF signature\nbyte[] head = new byte[12];\nstream.ReadExactly(head);\nbool ok = head[0..4] is \"RIFF\" && head[8..12] is \"WEBP\";\nstream.Position = 0;","typeGuard":null,"tryCatchPattern":"try { var info = await Image.IdentifyAsync(stream); }\ncatch (ImageFormatException ex) { log.Warn(ex, \"WebP chunk size unreadable (truncated?)\"); }","preventionTips":["Check file completeness before decode","Never decode streams being written concurrently","Validate RIFF/WEBP signatures on untrusted input"],"tags":["webp","riff","truncated-data","chunk"],"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"}