{"record":{"id":"f1d8a20810b459ca","repo":"SixLabors/ImageSharp","slug":"wrap-the-last-buffer-is-too-large","errorCode":null,"errorMessage":"Wrap: the last buffer is too large!","messagePattern":"Wrap: the last buffer is too large!","errorType":"exception","errorClass":"InvalidMemoryOperationException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs","lineNumber":239,"sourceCode":"\n        memoryGroup = new Owned(pool, arrays, bufferLength, totalLengthInElements, sizeOfLastBuffer, options);\n        return true;\n    }\n\n    public static MemoryGroup<T> Wrap(params Memory<T>[] source)\n    {\n        int bufferLength = source.Length > 0 ? source[0].Length : 0;\n        for (int i = 1; i < source.Length - 1; i++)\n        {\n            if (source[i].Length != bufferLength)\n            {\n                throw new InvalidMemoryOperationException(\"Wrap: buffers should be uniformly sized!\");\n            }\n        }\n\n        if (source.Length > 0 && source[^1].Length > bufferLength)\n        {\n            throw new InvalidMemoryOperationException(\"Wrap: the last buffer is too large!\");\n        }\n\n        long totalLength = bufferLength > 0 ? ((long)bufferLength * (source.Length - 1)) + source[^1].Length : 0;\n\n        return new Consumed(source, bufferLength, totalLength);\n    }\n\n    public static MemoryGroup<T> Wrap(params IMemoryOwner<T>[] source)\n    {\n        int bufferLength = source.Length > 0 ? source[0].Memory.Length : 0;\n        for (int i = 1; i < source.Length - 1; i++)\n        {\n            if (source[i].Memory.Length != bufferLength)\n            {\n                throw new InvalidMemoryOperationException(\"Wrap: buffers should be uniformly sized!\");\n            }\n        }\n","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs#L221-L257","documentation":"MemoryGroup<T>.Wrap allows the last buffer to be shorter than the uniform buffer length but not longer, since total group length is computed as (Count-1)*bufferLength + last.Length and oversized tails would corrupt the layout. A too-large last buffer raises InvalidMemoryOperationException.","triggerScenarios":"Calling MemoryGroup<T>.Wrap where source[^1].Length exceeds the uniform length of the preceding buffers (and Count > 0).","commonSituations":"Appending one large pooled buffer after several smaller ones when assembling wrapped memory for an image.","solutions":["Slice the last buffer down to at most the common buffer length before wrapping","Rent the final buffer with length <= bufferLength","Reorder so the smallest buffer is last"],"exampleFix":"// before\nmemoryGroup = MemoryGroup<T>.Wrap(new[] { b1(100), b2(100), big(200) });\n// after\nmemoryGroup = MemoryGroup<T>.Wrap(new[] { b1(100), b2(100), big.Slice(0, 100) });","handlingStrategy":"validation","validationCode":"if (source[^1].Length > source[0].Length) throw new InvalidOperationException(\"Last wrapped buffer exceeds uniform length.\");","typeGuard":"bool lastFits<T>(ReadOnlyMemory<T>[] s) => s.Length == 0 || s[^1].Length <= s[0].Length;","tryCatchPattern":"try { group = MemoryGroup<T>.Wrap(buffers); } catch (InvalidMemoryOperationException) { /* slice last buffer */ }","preventionTips":["Slice the final buffer to <= the uniform length before wrapping","Keep the smallest allocation last in the buffer list","Compute total required length up front and allocate accordingly"],"tags":["csharp","memory","buffers"],"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-23T08:17:48.524Z"}