{"record":{"id":"93bfcb779cbfc0d2","repo":"SixLabors/ImageSharp","slug":"invalid-webp-data-could-not-read-unsigned-24-bit-integer","errorCode":null,"errorMessage":"Invalid Webp data, could not read unsigned 24 bit integer.","messagePattern":"Invalid Webp data, could not read unsigned 24 bit integer\\.","errorType":"exception","errorClass":"ImageFormatException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Webp/WebpChunkParsingUtils.cs","lineNumber":273,"sourceCode":"\n    /// <summary>\n    /// Reads a unsigned 24 bit integer.\n    /// </summary>\n    /// <param name=\"stream\">The stream to read from.</param>\n    /// <param name=\"buffer\">The buffer to store the read data into.</param>\n    /// <returns>A unsigned 24 bit integer.</returns>\n    /// <exception cref=\"ImageFormatException\">\n    /// Thrown if the input stream is not valid.\n    /// </exception>\n    public static uint ReadUInt24LittleEndian(BufferedReadStream stream, Span<byte> buffer)\n    {\n        if (stream.Read(buffer, 0, 3) == 3)\n        {\n            buffer[3] = 0;\n            return BinaryPrimitives.ReadUInt32LittleEndian(buffer);\n        }\n\n        throw new ImageFormatException(\"Invalid Webp data, could not read unsigned 24 bit integer.\");\n    }\n\n    /// <summary>\n    /// Writes a unsigned 24 bit integer.\n    /// </summary>\n    /// <param name=\"stream\">The stream to write to.</param>\n    /// <param name=\"data\">The uint24 data to write.</param>\n    /// <exception cref=\"InvalidDataException\">\n    /// Thrown if the data is not a valid unsigned 24 bit integer.\n    /// </exception>\n    public static unsafe void WriteUInt24LittleEndian(Stream stream, uint data)\n    {\n        if (data >= 1 << 24)\n        {\n            throw new InvalidDataException($\"Invalid data, {data} is not a unsigned 24 bit integer.\");\n        }\n\n        uint* ptr = &data;","sourceCodeStart":255,"sourceCodeEnd":291,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Webp/WebpChunkParsingUtils.cs#L255-L291","documentation":"WebpChunkParsingUtils.ReadUInt24LittleEndian throws ImageFormatException when it cannot read 3 bytes from the stream while parsing a WebP VP8/VP8L/VP8X header field (width/height). A short read means the stream ended or is truncated mid-header, so the bitstream is incomplete. The decoder treats this as invalid content.","triggerScenarios":"Decoding a WebP whose file ends inside the VP8/VP8L frame header — ReadUInt24LittleEndian's stream.Read returns fewer than 3 bytes; called from width/height parsers during header decoding.","commonSituations":"Truncated downloads (interrupted transfers); files cropped by an upload size limit; streams where the caller already read part of the data; using Identify on a partially written file being downloaded.","solutions":["Re-obtain or re-download the file; compare length against the expected size.","Wrap decoding in try/catch (ImageFormatException) and treat as corrupt input.","If reading from a network stream, buffer the entire payload into a MemoryStream before decoding.","Check upstream code doesn't consume stream bytes before passing it to the decoder."],"exampleFix":"// before\nusing var image = Image.Load(networkStream); // may hit short-read mid-header\n// after\nusing var buffered = new MemoryStream();\nnetworkStream.CopyTo(buffered);\nbuffered.Position = 0;\nusing var image = Image.Load(buffered);","handlingStrategy":"try-catch","validationCode":"// ensure full payload before decoding\nusing var ms = new MemoryStream();\nsourceStream.CopyTo(ms);\nif (ms.Length < 30) throw new InvalidDataException(\"WebP too small/truncated\");\nms.Position = 0;","typeGuard":null,"tryCatchPattern":"try { using var image = Image.Load(webpStream); }\ncatch (ImageFormatException ex) { log.Warn(ex, \"Truncated WebP data\"); return null; }","preventionTips":["Fully buffer network streams before decoding","Verify download completeness (Content-Length vs actual bytes)","Catch ImageFormatException at every untrusted-input decode site"],"tags":["webp","truncated-data","decoding","stream"],"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"}