SixLabors/ImageSharp · error · InvalidImageContentException

An ANI RIFF chunk extends beyond its containing list.

Error message

An ANI RIFF chunk extends beyond its containing list.

What it means

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.

Solutions

  1. Fix or re-obtain the ANI so chunk sizes stay within their parent list bounds.
  2. Validate RIFF chunk sizes with a chunk-walking inspector before decoding.
  3. Catch InvalidImageContentException and reject the file as malformed.
Defensive patterns

Strategy: validation

Validate before calling

// Walk RIFF chunks and confirm each chunk's payload stays inside its parent list before decoding.
// Chunk end = pos + 8 + size (plus pad for odd size); must be <= parent list end.

Try / catch

try { var image = Image.Load(path); } catch (InvalidImageContentException ex) when (ex.Message.Contains("extends beyond")) { /* file has oversized chunk sizes */ }

Prevention

When it happens

Trigger: Decoding an ANI file where any RIFF chunk's 32-bit size field implies a payload end past the parent list's end offset.

Common situations: Corrupted chunk size fields from truncated/edited files, hand-crafted ANI files, fuzzed inputs with oversized size values, checked overflow scenarios.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13). Data as JSON: /api/errors/c6e5bdc4a9b2871b. Report an issue: GitHub.

Appendix: source

Thrown at src/ImageSharp/Formats/Ani/AniDecoderCore.cs:817

    {
        Span<byte> data = this.buffer[..AniConstants.ChunkHeaderSize];
        ReadExactly(stream, data, "RIFF chunk header");
        return AniRiffChunkHeader.Parse(data);
    }

    /// <summary>
    /// Calculates and validates the exclusive end of a RIFF chunk payload.
    /// </summary>
    /// <param name="stream">The ANI stream.</param>
    /// <param name="size">The declared payload size.</param>
    /// <param name="containerEnd">The exclusive parent-container boundary.</param>
    /// <returns>The exclusive payload boundary.</returns>
    private static long GetChunkDataEnd(BufferedReadStream stream, uint size, long containerEnd)
    {
        long end = checked(stream.Position + size);
        if (end > containerEnd)
        {
            throw new InvalidImageContentException("An ANI RIFF chunk extends beyond its containing list.");
        }

        return end;
    }

    /// <summary>
    /// Calculates and validates the word-aligned end of a RIFF chunk.
    /// </summary>
    /// <param name="dataEnd">The exclusive payload boundary.</param>
    /// <param name="size">The declared payload size.</param>
    /// <param name="containerEnd">The exclusive parent-container boundary.</param>
    /// <returns>The exclusive padded chunk boundary.</returns>
    private static long GetPaddedEnd(long dataEnd, uint size, long containerEnd)
    {
        // RIFF aligns each chunk to a 16-bit boundary without including the optional pad byte in the declared size.
        long paddedEnd = dataEnd + (size & 1);
        if (paddedEnd > containerEnd)
        {

View on GitHub (pinned to 59ce6af6fc)