Skip to content

Bug in WindowsRuntimeBufferExtensions.CopyTo extension throwing Destination too short #1808

Closed

Description

Describe the bug
When targeting 10.0.26100.38 or later, WindowsRuntimeBufferExtensions.CopyTo throws an exception (Destination too short) when copying one IBuffer to another, if the destination buffer is smaller than the source's Capacity.

This is probably an argument validation gone too far!

In earlier versions, the CopyTo will succeed if the destination buffer's Capacity is equal to or larger than the Length of the portion of the source buffer being copied.

To Reproduce
Please see simple application:
https://github.com/bjorn-malmo/bug-free-happiness

The gist of it:
Having 10.0.26100.45 in the project file (to target Windows App SDK 1.6) breaks this code:

using System.Runtime.InteropServices.WindowsRuntime;

var sourceBuffer = new Windows.Storage.Streams.Buffer(128000);
sourceBuffer.Length = 20000;
var destBuffer = new Windows.Storage.Streams.Buffer(20000);
destBuffer.Length = 0;

// This will work using .34 but not .38
// Please see the reference in the Project file
sourceBuffer.CopyTo(0, destBuffer, 0, 1000);

Removing the line from the project (thus targeting .34), will make the code sample succeed.

Expected behavior
The request to copy 1000 bytes into a buffer of size 20000 should succeed.

Version Info
Microsoft.Windows.SDK.NET.Ref version 10.0.26100.45

Additional context
This is a serious problem for us since it breaks downloading bitmap images in a WinUI 3 application, since the buffers allocated by BitmapImage is only as large as the expected data, and the data is part of a larger IBuffer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions