{"record":{"id":"ddbde4d67569728b","repo":"SixLabors/ImageSharp","slug":"the-embedded-ani-frame-resource-contains-an-invalid-seek","errorCode":null,"errorMessage":"The embedded ANI frame resource contains an invalid seek offset.","messagePattern":"The embedded ANI frame resource contains an invalid seek offset\\.","errorType":"exception","errorClass":"InvalidImageContentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Ani/AniFrameStream.cs","lineNumber":101,"sourceCode":"\n        return read;\n    }\n\n    /// <inheritdoc/>\n    public override long Seek(long offset, SeekOrigin origin)\n    {\n        long target = origin switch\n        {\n            SeekOrigin.Begin => offset,\n            SeekOrigin.Current => this.position + offset,\n            SeekOrigin.End => this.length + offset,\n            _ => throw new ArgumentOutOfRangeException(nameof(origin))\n        };\n\n        // Casting rejects both negative offsets and offsets beyond Length with one bounds check.\n        if ((ulong)target > (ulong)this.length)\n        {\n            throw new InvalidImageContentException(\"The embedded ANI frame resource contains an invalid seek offset.\");\n        }\n\n        // Delay moving the containing stream until Read; this keeps logical seeks isolated from sibling resource processing.\n        this.position = target;\n        return target;\n    }\n\n    /// <inheritdoc/>\n    public override void SetLength(long value) => throw new NotSupportedException();\n\n    /// <inheritdoc/>\n    public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();\n}\n","sourceCodeStart":83,"sourceCodeEnd":115,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Ani/AniFrameStream.cs#L83-L115","documentation":"AniFrameStream is a read-only view over one embedded ANI frame resource. Seek validates the computed target against the resource length with a single unsigned bounds check; a negative offset or a position past the end throws InvalidImageContentException, indicating the resource bytes or a caller-computed offset are invalid.","triggerScenarios":"Calling Seek on the frame stream with an origin/offset combination yielding target < 0 or target > Length — e.g. Seek(offset, SeekOrigin.Current) with an offset that overruns the resource, or corrupt RIFF 'data' chunk sizes making Length smaller than expected.","commonSituations":"Parsing hand-built or truncated ANI files where chunk sizes don't match actual bytes; third-party code positioning the stream with stale offsets after the resource was replaced with a shorter one.","solutions":["Clamp or validate offsets against stream.Length before calling Seek.","Re-extract the ANI file; the embedded resource sizes may be corrupt.","Use SeekOrigin.Begin with absolute offsets computed from the resource start."],"exampleFix":"// before\nframeStream.Seek(frameStream.Position + advance, SeekOrigin.Current); // may overrun\n// after\nlong target = frameStream.Position + advance;\nif (target >= 0 && target <= frameStream.Length) frameStream.Seek(target, SeekOrigin.Begin);","handlingStrategy":"validation","validationCode":"long target = frameStream.Position + advance;\nif (target < 0 || target > frameStream.Length) throw new InvalidOperationException(\"Seek target out of resource bounds.\");","typeGuard":null,"tryCatchPattern":"try { frameStream.Seek(offset, SeekOrigin.Begin); }\ncatch (InvalidImageContentException) { /* resource offsets/sizes are corrupt; re-extract file */ }","preventionTips":["Compute offsets from chunk boundaries of the same resource, not the outer stream.","Re-verify chunk sizes when parsing RIFF/ANI containers.","Prefer SeekOrigin.Begin with absolute offsets."],"tags":["ani","stream","corrupt-data"],"backgroundTag":"invalid-argument-value","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"}