Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix capacity checks in BufferExtensions #504

Merged
merged 18 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions UnitTest/TestComponentCSharp_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,150 @@ public void TestEmptyBufferToArray()
}

[Fact]
public void TestBufferCheck1()
{
IBuffer buf = new Windows.Storage.Streams.Buffer(3);
byte[] arr = new byte[] { 0x01, 0x02, 0x03 };
// Do something with buffer and arr
// CopyTo(this byte[] source, int sourceIndex, IBuffer destination, uint destinationIndex, int count)
arr.CopyTo(3, buf, 3, 0);
}
[Fact(Skip="errors properly")]
public void TestBufferCheck11()
{
IBuffer buf = new Windows.Storage.Streams.Buffer(3);
byte[] arr = new byte[] { 0x01, 0x02, 0x03 };
arr.CopyTo(3, buf, 3, 1); // errors
j0shuams marked this conversation as resolved.
Show resolved Hide resolved
}
[Fact]
public void TestBufferCheck2()
{
IBuffer buf = new Windows.Storage.Streams.Buffer(3);
// ToArray(this IBuffer source, uint sourceIndex, int count)
var arr = buf.ToArray(3, 0);
}

[Fact]
public void TestBufferCheck3()
{
IBuffer buf = new Windows.Storage.Streams.Buffer(3);
byte[] arr = new byte[] { 0x01, 0x02, 0x03 };
// CopyTo(this IBuffer source, uint sourceIndex, byte[] destination, int destinationIndex, int count)
buf.CopyTo(3, arr, 0, 0); // copy 0-span from end of buf to front of array
}

[Fact]
public void TestBufferCheck32()
{
IBuffer buf = new Windows.Storage.Streams.Buffer(3);
byte[] arr = new byte[] { 0x01, 0x02, 0x03 };
// CopyTo(this IBuffer source, uint sourceIndex, byte[] destination, int destinationIndex, int count)
buf.CopyTo(3, arr, 0, 1); // copy 0-span from end of buf to front of array
}

[Fact]
public void TestBufferCheck31()
{
IBuffer buf = new Windows.Storage.Streams.Buffer(3);
byte[] arr = new byte[] { 0x01, 0x02, 0x03 };
// CopyTo(this IBuffer source, uint sourceIndex, byte[] destination, int destinationIndex, int count)
buf.CopyTo(3, arr, 3, 0); // copy 0-span from end of buf to end of array
}
[Fact]
public void TestBufferCheck33()
{
IBuffer buf = new Windows.Storage.Streams.Buffer(3);
byte[] arr = new byte[] { 0x01, 0x02, 0x03 };
// CopyTo(this IBuffer source, uint sourceIndex, byte[] destination, int destinationIndex, int count)
buf.CopyTo(3, arr, 3, 1); // copy 0-span from end of buf to end of array
}

[Fact]
public void TestBufferCheck4()
{
IBuffer buf = new Windows.Storage.Streams.Buffer(3);
IBuffer dest = new Windows.Storage.Streams.Buffer(3);
// CopyTo(this IBuffer source, uint sourceIndex, IBuffer destination, uint destinationIndex, uint count)
buf.CopyTo(3, dest, 3, 0);
}

[Fact]
public void TestBufferCheck43()
{
IBuffer buf = new Windows.Storage.Streams.Buffer(3);
IBuffer dest = new Windows.Storage.Streams.Buffer(3);
// CopyTo(this IBuffer source, uint sourceIndex, IBuffer destination, uint destinationIndex, uint count)
buf.CopyTo(3, dest, 3, 1);
}

[Fact]
public void TestBufferCheck41()
{
IBuffer buf = new Windows.Storage.Streams.Buffer(3);
IBuffer dest = new Windows.Storage.Streams.Buffer(3);
// CopyTo(this IBuffer source, uint sourceIndex, IBuffer destination, uint destinationIndex, uint count)
buf.CopyTo(3, dest, 1, 0);
}

[Fact]
public void TestBufferCheck44()
{
IBuffer buf = new Windows.Storage.Streams.Buffer(3);
IBuffer dest = new Windows.Storage.Streams.Buffer(3);
// CopyTo(this IBuffer source, uint sourceIndex, IBuffer destination, uint destinationIndex, uint count)
buf.CopyTo(3, dest, 1, 1);
}

[Fact]
public void TestBufferCheck42()
{
IBuffer buf = new Windows.Storage.Streams.Buffer(3);
IBuffer dest = new Windows.Storage.Streams.Buffer(3);
// CopyTo(this IBuffer source, uint sourceIndex, IBuffer destination, uint destinationIndex, uint count)
buf.CopyTo(1, dest, 1, 0);
}

[Fact]
public void TestBufferCheck45()
{
IBuffer buf = new Windows.Storage.Streams.Buffer(3);
IBuffer dest = new Windows.Storage.Streams.Buffer(3);
// CopyTo(this IBuffer source, uint sourceIndex, IBuffer destination, uint destinationIndex, uint count)
buf.CopyTo(1, dest, 1, 1);
}

[Fact]
public void TestBufferCheck5()
{
IBuffer buf = new Windows.Storage.Streams.Buffer(3);
byte[] arr = new byte[] { 0x01, 0x02, 0x03 };
// Do something with buffer and arr
// GetByte(this IBuffer source, uint byteOffset)
buf.GetByte(3); // think this will error
}

[Fact]
public void TestBufferCheck6()
{
MemoryStream stream = new MemoryStream(3);
IBuffer buf = stream.GetWindowsRuntimeBuffer(2, 0);
}

[Fact]
public void TestBufferCheck61()
{
MemoryStream stream = new MemoryStream(3);
IBuffer buff = stream.GetWindowsRuntimeBuffer(3, 0);
}

[Fact]
public void TestBufferCheck62()
{
MemoryStream stream = new MemoryStream(3);
IBuffer buff = stream.GetWindowsRuntimeBuffer(3, 1);
}

[Fact(Skip ="blocked by issue #497")]
public void TestEmptyBufferCopyTo()
{
var buffer = new Windows.Storage.Streams.Buffer(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static void CopyTo(this byte[] source, int sourceIndex, IBuffer destinati
if (destination == null) throw new ArgumentNullException(nameof(destination));
if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));
if (sourceIndex < 0) throw new ArgumentOutOfRangeException(nameof(sourceIndex));
if (source.Length <= sourceIndex) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_IndexOutOfArrayBounds, nameof(sourceIndex));
if (source.Length < sourceIndex) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_IndexOutOfArrayBounds, nameof(sourceIndex));
j0shuams marked this conversation as resolved.
Show resolved Hide resolved
if (source.Length - sourceIndex < count) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_InsufficientArrayElementsAfterOffset);
if (destination.Capacity - destinationIndex < count) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_InsufficientSpaceInTargetBuffer);

Expand Down Expand Up @@ -118,9 +118,8 @@ public static byte[] ToArray(this IBuffer source)
public static byte[] ToArray(this IBuffer source, uint sourceIndex, int count)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (source.Length == 0 && count == 0) return Array.Empty<byte>();
if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));
if (source.Capacity <= sourceIndex) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_BufferIndexExceedsCapacity);
if (source.Capacity < sourceIndex) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_BufferIndexExceedsCapacity);
j0shuams marked this conversation as resolved.
Show resolved Hide resolved
if (source.Capacity - sourceIndex < count) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_InsufficientSpaceInSourceBuffer);

if (count == 0)
Expand All @@ -141,9 +140,6 @@ public static void CopyTo(this IBuffer source, byte[] destination)
if (source == null) throw new ArgumentNullException(nameof(source));
if (destination == null) throw new ArgumentNullException(nameof(destination));

// If buffer is empty, nothing to copy
if (source.Length == 0) return;

CopyTo(source, 0, destination, 0, checked((int)source.Length));
}

Expand All @@ -154,10 +150,9 @@ public static void CopyTo(this IBuffer source, uint sourceIndex, byte[] destinat
if (destination == null) throw new ArgumentNullException(nameof(destination));
if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));
if (destinationIndex < 0) throw new ArgumentOutOfRangeException(nameof(destinationIndex));
if (source.Length == 0 && count == 0) return;
if (source.Capacity <= sourceIndex) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_BufferIndexExceedsCapacity);
if (source.Capacity < sourceIndex) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_BufferIndexExceedsCapacity);
j0shuams marked this conversation as resolved.
Show resolved Hide resolved
if (source.Capacity - sourceIndex < count) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_InsufficientSpaceInSourceBuffer);
if (destination.Length <= destinationIndex) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_IndexOutOfArrayBounds);
if (destination.Length < destinationIndex) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_IndexOutOfArrayBounds);
if (destination.Length - destinationIndex < count) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_InsufficientArrayElementsAfterOffset);

// If source is backed by a managed array, use the array instead of the pointer as it does not require pinning:
Expand Down Expand Up @@ -191,10 +186,9 @@ public static void CopyTo(this IBuffer source, uint sourceIndex, IBuffer destina
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (destination == null) throw new ArgumentNullException(nameof(destination));
if (count == 0) return;
if (source.Capacity <= sourceIndex) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_BufferIndexExceedsCapacity);
if (source.Capacity < sourceIndex) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_BufferIndexExceedsCapacity);
j0shuams marked this conversation as resolved.
Show resolved Hide resolved
if (source.Capacity - sourceIndex < count) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_InsufficientSpaceInSourceBuffer);
if (destination.Capacity <= destinationIndex) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_BufferIndexExceedsCapacity);
if (destination.Capacity < destinationIndex) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_BufferIndexExceedsCapacity);
if (destination.Capacity - destinationIndex < count) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_InsufficientSpaceInTargetBuffer);

// If source are destination are backed by managed arrays, use the arrays instead of the pointers as it does not require pinning:
Expand Down Expand Up @@ -381,7 +375,7 @@ public static IBuffer GetWindowsRuntimeBuffer(this MemoryStream underlyingStream
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length));

if (underlyingStream.Capacity <= positionInStream)
if (underlyingStream.Capacity < positionInStream)
j0shuams marked this conversation as resolved.
Show resolved Hide resolved
throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_StreamPositionBeyondEOS);

ArraySegment<byte> streamData;
Expand Down Expand Up @@ -426,7 +420,7 @@ public static Stream AsStream(this IBuffer source)
public static byte GetByte(this IBuffer source, uint byteOffset)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (source.Capacity <= byteOffset) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_BufferIndexExceedsCapacity, nameof(byteOffset));
if (source.Capacity < byteOffset) throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_BufferIndexExceedsCapacity, nameof(byteOffset));
j0shuams marked this conversation as resolved.
Show resolved Hide resolved

byte[] srcDataArr;
int srcDataOffs;
Expand Down