SixLabors/ImageSharp · error · InvalidOperationException

Column regions should not overlap!

Error message

Column regions should not overlap!

What it means

Buffer2DExtensions.DangerousCopyColumns validates that the source and destination column regions neither overlap each other nor extend past the buffer edges. Overlapping column copies cannot be performed safely with the single-pass copy used, so InvalidOperationException is thrown.

Solutions

  1. Validate that maxIndex < minIndex + columnCount and maxIndex <= buffer.Width - columnCount before calling
  2. Copy to a temporary buffer first when overlapping shifts are intended
  3. Reduce columnCount so the regions are disjoint

Example fix

// before
buffer.DangerousCopyColumns(0, 3, 5);
// after
if (Math.Max(0, 3) < 3 + 5) { /* copy via temp buffer or adjust indices */ }
buffer.DangerousCopyColumns(0, 5, 5);
Defensive patterns

Strategy: validation

Validate before calling

bool ok = Math.Max(src, dst) < Math.Min(src, dst) + count && Math.Max(src, dst) <= buffer.Width - count;

Type guard

bool canCopyColumns<T>(Buffer2D<T> b, int src, int dst, int count) => Math.Max(src, dst) < Math.Min(src, dst) + count && Math.Max(src, dst) <= b.Width - count;

Try / catch

try { buffer.DangerousCopyColumns(src, dst, count); } catch (InvalidOperationException) { /* use temp-buffer copy */ }

Prevention

When it happens

Trigger: Calling DangerousCopyColumns/CopyColumns where sourceIndex and destIndex ranges overlap (e.g. copy 5 columns from x=0 to x=3) or where maxIndex + columnCount exceeds buffer.Width.

Common situations: Implementing custom pixel-shifting/sliding operations on image columns with ranges computed at runtime.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/ImageSharp/Memory/Buffer2DExtensions.cs:153

    /// <param name="buffer">The <see cref="Buffer2D{T}"/></param>
    /// <returns>The <see cref="Buffer2DRegion{T}"/></returns>
    public static Buffer2DRegion<T> GetRegion<T>(this Buffer2D<T> buffer)
        where T : unmanaged =>
        new(buffer);

    [Conditional("DEBUG")]
    private static void CheckColumnRegionsDoNotOverlap<T>(
        Buffer2D<T> buffer,
        int sourceIndex,
        int destIndex,
        int columnCount)
        where T : struct
    {
        int minIndex = Math.Min(sourceIndex, destIndex);
        int maxIndex = Math.Max(sourceIndex, destIndex);
        if (maxIndex < minIndex + columnCount || maxIndex > buffer.Width - columnCount)
        {
            throw new InvalidOperationException("Column regions should not overlap!");
        }
    }
}

View on GitHub (pinned to 59ce6af6fc)