{"record":{"id":"8f7b55c761d598f3","repo":"SixLabors/ImageSharp","slug":"dangerousgetrowspan-y-y-was-out-of-range-height-this-height","errorCode":null,"errorMessage":"DangerousGetRowSpan({y}). Y was out of range. Height={this.Height}","messagePattern":"DangerousGetRowSpan\\((.+?)\\)\\. Y was out of range\\. Height=(.+?)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Memory/Buffer2D{T}.cs","lineNumber":422,"sourceCode":"            bool destinationCanRepresentSource = destination.FastMemoryGroup.TotalLength >= sourceLayoutLength;\n            bool sourceCanRepresentDestination = source.FastMemoryGroup.TotalLength >= destinationLayoutLength;\n            if (!destinationCanRepresentSource || !sourceCanRepresentDestination)\n            {\n                throw new InvalidMemoryOperationException(\n                    \"Trying to copy/swap incompatible buffers. This is most likely caused by applying an unsupported processor to wrapped-memory images.\");\n            }\n\n            source.CopyTo(destination);\n        }\n\n        (destination.Size, source.Size) = (source.Size, destination.Size);\n        (destination.RowStride, source.RowStride) = (source.RowStride, destination.RowStride);\n        return swapped;\n    }\n\n    [MethodImpl(InliningOptions.ColdPath)]\n    private void ThrowYOutOfRangeException(int y)\n        => throw new ArgumentOutOfRangeException($\"DangerousGetRowSpan({y}). Y was out of range. Height={this.Height}\");\n\n    [MethodImpl(InliningOptions.ShortMethod)]\n    private static long GetRequiredLength(int width, int height, int stride)\n        => checked(((long)(height - 1) * stride) + width);\n}\n","sourceCodeStart":404,"sourceCodeEnd":428,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Memory/Buffer2D{T}.cs#L404-L428","documentation":"Buffer2D<T>.DangerousGetRowSpan validates the requested row index against the buffer's Height and throws ArgumentOutOfRangeException naming the offending y and the height when it is outside [0, Height).","triggerScenarios":"Calling DangerousGetRowSpan with y < 0 or y >= buffer.Height, often from loop bounds computed from a different buffer or an off-by-one (y <= Height).","commonSituations":"Manual pixel loops over ProcessedPixelRow callbacks or custom processors where the row index comes from another image's dimensions.","solutions":["Clamp or verify y against buffer.Height before the call","Fix loop bounds to 'for (int y = 0; y < buffer.Height; y++)'","Use the buffer's own span enumeration instead of manual indexing"],"exampleFix":"// before\nfor (int y = 0; y <= buffer.Height; y++) Process(buffer.DangerousGetRowSpan(y));\n// after\nfor (int y = 0; y < buffer.Height; y++) Process(buffer.DangerousGetRowSpan(y));","handlingStrategy":"validation","validationCode":"if (y < 0 || y >= buffer.Height) throw new ArgumentOutOfRangeException(nameof(y));","typeGuard":"bool validRow<T>(Buffer2D<T> b, int y) => (uint)y < (uint)b.Height;","tryCatchPattern":"try { var span = buffer.DangerousGetRowSpan(y); } catch (ArgumentOutOfRangeException) { /* clamp y and retry */ }","preventionTips":["Use strict '<' comparisons with the same buffer's Height in loops","Derive y from the buffer itself, not a different image","Prefer foreach over buffer spans to avoid manual indexing"],"tags":["csharp","memory","indexing"],"backgroundTag":"index-out-of-range","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}