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 access beyond array boundaries in SpanByte #140

Merged
merged 2 commits into from
May 24, 2021
Merged
Changes from all 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
16 changes: 7 additions & 9 deletions nanoFramework.CoreLibrary/System/SpanByte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public SpanByte(byte[] array, int start, int length)
{
if (array != null)
{
if ((length > array.Length - start) || (start > array.Length))
if (start < 0 ||
length < 0 ||
start + length > array.Length ||
(start == array.Length && start > 0))
{
throw new ArgumentOutOfRangeException($"Array length too small");
}
Expand All @@ -69,7 +72,7 @@ public byte this[int index]
{
get
{
if (index > _length)
if (index >= _length)
{
throw new ArgumentOutOfRangeException($"Index out of range");
}
Expand All @@ -78,7 +81,7 @@ public byte this[int index]
}
set
{
if (index > _length)
if (index >= _length)
{
throw new ArgumentOutOfRangeException($"Index out of range");
}
Expand Down Expand Up @@ -131,12 +134,7 @@ public void CopyTo(SpanByte destination)
/// <exception cref="System.ArgumentOutOfRangeException">start is less than zero or greater than System.Span.Length.</exception>
public SpanByte Slice(int start)
{
if ((start > _length) || (start < 0))
{
throw new ArgumentOutOfRangeException($"start is less than zero or greater than length");
}

return new SpanByte(_array, _start + start, _length - start);
return Slice(start, _length - start);
}

/// <summary>
Expand Down