Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 21b48fd

Browse files
committed
Add bounds checks for offset in classes that impl OwnedMemory
1 parent 9351823 commit 21b48fd

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/Common/tests/System/Buffers/NativeOwnedMemory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ protected override bool IsRetained
5555

5656
public override unsafe MemoryHandle Pin(int offset = 0)
5757
{
58+
if (offset < 0 || offset >= _length) throw new ArgumentOutOfRangeException(nameof(offset));
5859
void* pointer = (void*)((byte*)_ptr + offset);
59-
return new MemoryHandle(this, _ptr);
60+
return new MemoryHandle(this, pointer);
6061
}
6162

6263
public override bool Release()

src/System.Memory/tests/Memory/CustomMemoryForTest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ public override Span<T> Span
3939
}
4040
}
4141

42-
public override MemoryHandle Pin(int index = 0)
42+
public override MemoryHandle Pin(int offset = 0)
4343
{
4444
unsafe
4545
{
4646
Retain();
47+
if (offset < 0 || offset >= _array.Length) throw new ArgumentOutOfRangeException(nameof(offset));
4748
var handle = GCHandle.Alloc(_array, GCHandleType.Pinned);
48-
return new MemoryHandle(this, Unsafe.Add<byte>((void*)handle.AddrOfPinnedObject(), index), handle);
49+
return new MemoryHandle(this, Unsafe.Add<byte>((void*)handle.AddrOfPinnedObject(), offset), handle);
4950
}
5051
}
5152

0 commit comments

Comments
 (0)