{"record":{"id":"c6e5bdc4a9b2871b","repo":"SixLabors/ImageSharp","slug":"an-ani-riff-chunk-extends-beyond-its-containing-list","errorCode":null,"errorMessage":"An ANI RIFF chunk extends beyond its containing list.","messagePattern":"An ANI RIFF chunk extends beyond its containing list\\.","errorType":"exception","errorClass":"InvalidImageContentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Ani/AniDecoderCore.cs","lineNumber":817,"sourceCode":"    {\n        Span<byte> data = this.buffer[..AniConstants.ChunkHeaderSize];\n        ReadExactly(stream, data, \"RIFF chunk header\");\n        return AniRiffChunkHeader.Parse(data);\n    }\n\n    /// <summary>\n    /// Calculates and validates the exclusive end of a RIFF chunk payload.\n    /// </summary>\n    /// <param name=\"stream\">The ANI stream.</param>\n    /// <param name=\"size\">The declared payload size.</param>\n    /// <param name=\"containerEnd\">The exclusive parent-container boundary.</param>\n    /// <returns>The exclusive payload boundary.</returns>\n    private static long GetChunkDataEnd(BufferedReadStream stream, uint size, long containerEnd)\n    {\n        long end = checked(stream.Position + size);\n        if (end > containerEnd)\n        {\n            throw new InvalidImageContentException(\"An ANI RIFF chunk extends beyond its containing list.\");\n        }\n\n        return end;\n    }\n\n    /// <summary>\n    /// Calculates and validates the word-aligned end of a RIFF chunk.\n    /// </summary>\n    /// <param name=\"dataEnd\">The exclusive payload boundary.</param>\n    /// <param name=\"size\">The declared payload size.</param>\n    /// <param name=\"containerEnd\">The exclusive parent-container boundary.</param>\n    /// <returns>The exclusive padded chunk boundary.</returns>\n    private static long GetPaddedEnd(long dataEnd, uint size, long containerEnd)\n    {\n        // RIFF aligns each chunk to a 16-bit boundary without including the optional pad byte in the declared size.\n        long paddedEnd = dataEnd + (size & 1);\n        if (paddedEnd > containerEnd)\n        {","sourceCodeStart":799,"sourceCodeEnd":835,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Ani/AniDecoderCore.cs#L799-L835","documentation":"Thrown by AniDecoderCore.GetChunkDataEnd when a RIFF chunk's declared size makes its payload end (stream.Position + size) exceed the containing list's exclusive end boundary. This means the chunk header lies about its size relative to its parent list. The library enforces container containment to avoid reading beyond the list.","triggerScenarios":"Decoding an ANI file where any RIFF chunk's 32-bit size field implies a payload end past the parent list's end offset.","commonSituations":"Corrupted chunk size fields from truncated/edited files, hand-crafted ANI files, fuzzed inputs with oversized size values, checked overflow scenarios.","solutions":["Fix or re-obtain the ANI so chunk sizes stay within their parent list bounds.","Validate RIFF chunk sizes with a chunk-walking inspector before decoding.","Catch InvalidImageContentException and reject the file as malformed."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Walk RIFF chunks and confirm each chunk's payload stays inside its parent list before decoding.\n// Chunk end = pos + 8 + size (plus pad for odd size); must be <= parent list end.","typeGuard":null,"tryCatchPattern":"try { var image = Image.Load(path); } catch (InvalidImageContentException ex) when (ex.Message.Contains(\"extends beyond\")) { /* file has oversized chunk sizes */ }","preventionTips":["Validate RIFF chunk containment with a chunk walker for untrusted files","Never hand-edit chunk size fields without recomputing parent sizes","Pre-screen with Image.IdentifyAsync"],"tags":["image-decoding","ani","riff","chunk-size","bounds-check"],"backgroundTag":"schema-validation-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"}