Skip to content

Commit

Permalink
ToArray fails when buffer capacity is larger than its length. (#1805)
Browse files Browse the repository at this point in the history
* Add failing test for ToArray.

* Add missing Slice call to limit length of span to count.

* Add another test.
  • Loading branch information
jlaanstra authored Oct 1, 2024
1 parent 3c1ba7f commit 2e56fca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/Tests/UnitTest/TestComponentCSharp_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,34 @@ public void TestEmptyBufferCopyTo()
Assert.True(array.Length == 0);
}

[Fact]
public void TestBufferToArrayCapacityLargerThanLength()
{
var buffer = new Windows.Storage.Streams.Buffer(100);
byte[] arr = new byte[] { 0x01, 0x02, 0x03 };
arr.CopyTo(0, buffer, 0, 3);

byte[] newArr = buffer.ToArray();
Assert.True(newArr.Length == 3);
}

[Fact]
public void TestBufferCopyToBufferCapacityLargerThanLength()
{
var buffer = new Windows.Storage.Streams.Buffer(100);
byte[] arr = new byte[] { 0x01, 0x02, 0x03 };
arr.CopyTo(0, buffer, 0, 3);

var buffer2 = new Windows.Storage.Streams.Buffer(50);
byte[] arr2 = new byte[] { 0x01, 0x02 };
arr2.CopyTo(0, buffer2, 0, 2);

Assert.True(buffer2.Length == 2);

buffer.CopyTo(buffer2);
Assert.True(buffer2.Length == 3);
}

#if NET
[Fact]
public void TestTryGetDataUnsafe()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public static void CopyTo(this IBuffer source, uint sourceIndex, Span<byte> dest

Debug.Assert(sourceIndex <= int.MaxValue);

Span<byte> srcSpan = source.TryGetUnderlyingData(out byte[] srcDataArr, out int srcOffset) ? srcDataArr.AsSpan(srcOffset + (int)sourceIndex, count) : source.GetSpanForCapacityUnsafe(sourceIndex);
Span<byte> srcSpan = source.TryGetUnderlyingData(out byte[] srcDataArr, out int srcOffset) ? srcDataArr.AsSpan(srcOffset + (int)sourceIndex, count) : source.GetSpanForCapacityUnsafe(sourceIndex).Slice(0, (int)count);
srcSpan.CopyTo(destination);

// Ensure source and destination stay alive for the copy operation
Expand Down Expand Up @@ -252,8 +252,8 @@ public static void CopyTo(this IBuffer source, uint sourceIndex, IBuffer destina
Debug.Assert(destinationIndex <= int.MaxValue);

// If source are destination are backed by managed arrays, use the arrays instead of the pointers as it does not require pinning:
Span<byte> srcSpan = source.TryGetUnderlyingData(out byte[] srcDataArr, out int srcOffset) ? srcDataArr.AsSpan(srcOffset + (int)sourceIndex, (int)count) : source.GetSpanForCapacityUnsafe(sourceIndex);
Span<byte> destSpan = destination.TryGetUnderlyingData(out byte[] destDataArr, out int destOffset) ? destDataArr.AsSpan(destOffset + (int)destinationIndex) : destination.GetSpanForCapacityUnsafe(destinationIndex);
Span<byte> srcSpan = source.TryGetUnderlyingData(out byte[] srcDataArr, out int srcOffset) ? srcDataArr.AsSpan(srcOffset + (int)sourceIndex, (int)count) : source.GetSpanForCapacityUnsafe(sourceIndex).Slice(0, (int)count);
Span<byte> destSpan = destination.TryGetUnderlyingData(out byte[] destDataArr, out int destOffset) ? destDataArr.AsSpan(destOffset + (int)destinationIndex) : destination.GetSpanForCapacityUnsafe(destinationIndex).Slice(0, (int)count);

srcSpan.CopyTo(destSpan);

Expand Down

0 comments on commit 2e56fca

Please sign in to comment.