Skip to content

Commit da9c46e

Browse files
authored
Fix access beyond array boundaries in SpanByte (#140)
***NO_CI***
1 parent 9869e58 commit da9c46e

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

nanoFramework.CoreLibrary/System/SpanByte.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ public SpanByte(byte[] array, int start, int length)
4242
{
4343
if (array != null)
4444
{
45-
if ((length > array.Length - start) || (start > array.Length))
45+
if (start < 0 ||
46+
length < 0 ||
47+
start + length > array.Length ||
48+
(start == array.Length && start > 0))
4649
{
4750
throw new ArgumentOutOfRangeException($"Array length too small");
4851
}
@@ -69,7 +72,7 @@ public byte this[int index]
6972
{
7073
get
7174
{
72-
if (index > _length)
75+
if (index >= _length)
7376
{
7477
throw new ArgumentOutOfRangeException($"Index out of range");
7578
}
@@ -78,7 +81,7 @@ public byte this[int index]
7881
}
7982
set
8083
{
81-
if (index > _length)
84+
if (index >= _length)
8285
{
8386
throw new ArgumentOutOfRangeException($"Index out of range");
8487
}
@@ -131,12 +134,7 @@ public void CopyTo(SpanByte destination)
131134
/// <exception cref="System.ArgumentOutOfRangeException">start is less than zero or greater than System.Span.Length.</exception>
132135
public SpanByte Slice(int start)
133136
{
134-
if ((start > _length) || (start < 0))
135-
{
136-
throw new ArgumentOutOfRangeException($"start is less than zero or greater than length");
137-
}
138-
139-
return new SpanByte(_array, _start + start, _length - start);
137+
return Slice(start, _length - start);
140138
}
141139

142140
/// <summary>

0 commit comments

Comments
 (0)