{"record":{"id":"dc4817ef2fac5bd8","repo":"SixLabors/ImageSharp","slug":"an-ani-riff-chunk-is-missing-its-alignment-padding","errorCode":null,"errorMessage":"An ANI RIFF chunk is missing its alignment padding.","messagePattern":"An ANI RIFF chunk is missing its alignment padding\\.","errorType":"exception","errorClass":"InvalidImageContentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Ani/AniDecoderCore.cs","lineNumber":836,"sourceCode":"        }\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        {\n            throw new InvalidImageContentException(\"An ANI RIFF chunk is missing its alignment padding.\");\n        }\n\n        return paddedEnd;\n    }\n\n    /// <summary>\n    /// Reads an exact number of bytes or reports a truncated ANI file.\n    /// </summary>\n    /// <param name=\"stream\">The ANI stream.</param>\n    /// <param name=\"destination\">The destination buffer.</param>\n    /// <param name=\"description\">The data description used in the error message.</param>\n    private static void ReadExactly(BufferedReadStream stream, Span<byte> destination, string description)\n    {\n        if (stream.Read(destination) != destination.Length)\n        {\n            throw new InvalidImageContentException($\"Not enough bytes to read the {description}.\");\n        }\n    }","sourceCodeStart":818,"sourceCodeEnd":854,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Ani/AniDecoderCore.cs#L818-L854","documentation":"Thrown by AniDecoderCore.GetPaddedEnd when the chunk's 16-bit-aligned padded end (dataEnd plus one pad byte for odd sizes) would exceed the container end. RIFF pads odd-sized chunks to even boundaries; the library requires that pad byte to exist inside the list. Missing padding means the file is structurally incomplete.","triggerScenarios":"Decoding an ANI file where an odd-sized chunk sits at the very end of its containing list with no alignment pad byte, so dataEnd + (size & 1) > containerEnd.","commonSituations":"Files truncated exactly at a chunk boundary (pad byte stripped), ANI files written by tools that omit RIFF padding, hand-edited files.","solutions":["Re-export the ANI with a compliant writer that emits RIFF pad bytes for odd-sized chunks.","Append the missing pad byte if editing the file manually.","Catch InvalidImageContentException and treat the file as structurally invalid."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// For each odd-sized chunk, verify one pad byte exists before the next chunk header.\nbool hasPadByte = chunkDataEnd + 1 <= containerEnd;","typeGuard":null,"tryCatchPattern":"try { var image = Image.Load(path); } catch (InvalidImageContentException ex) when (ex.Message.Contains(\"alignment padding\")) { /* writer omitted pad byte */ }","preventionTips":["Use RIFF-compliant writers that pad odd-sized chunks","Check for truncation when a file ends exactly at an odd chunk boundary","Run chunk-structure validation on generated ANI files"],"tags":["image-decoding","ani","riff","alignment","padding"],"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"}