From 77a44bd24110da51e364b60dbdfc95033ec4660f Mon Sep 17 00:00:00 2001 From: Chad Rockey Date: Thu, 11 Apr 2024 02:02:10 -0500 Subject: [PATCH 1/2] C# Fix issues with socket buffer sizes --- src/bindings/csharp/Socket.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/bindings/csharp/Socket.cs b/src/bindings/csharp/Socket.cs index 6903a334..57354b41 100644 --- a/src/bindings/csharp/Socket.cs +++ b/src/bindings/csharp/Socket.cs @@ -310,15 +310,15 @@ public Int32 Send(Byte[] buffer, int offset, int size, SocketFlags socketFlags) if (buffer == null) { throw new ArgumentNullException("buffer"); } - if (size < 0 || size > buffer.Length - offset) { + if (size <= 0 || size > buffer.Length - offset) { throw new ArgumentOutOfRangeException("size"); } - if (offset < 0 || offset > buffer.Length) { + if (offset < 0 || offset >= buffer.Length) { throw new ArgumentOutOfRangeException("offset"); } int flags = 0; IntPtr bufferPtr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0); - return zts_bsd_send(_fd, bufferPtr + offset, (uint)Buffer.ByteLength(buffer), (int)flags); + return zts_bsd_send(_fd, bufferPtr + offset, (uint)size, (int)flags); } public int Available @@ -344,10 +344,10 @@ public Int32 Receive(byte[] buffer, int offset, int size, SocketFlags socketFlag if (buffer == null) { throw new ArgumentNullException("buffer"); } - if (size < 0 || size > buffer.Length - offset) { + if (size <= 0 || size > buffer.Length - offset) { throw new ArgumentOutOfRangeException("size"); } - if (offset < 0 || offset > buffer.Length) { + if (offset < 0 || offset >= buffer.Length) { throw new ArgumentOutOfRangeException("offset"); } int flags = 0; From 251c2004f5dfe66385d0732ff8ac365c421a652e Mon Sep 17 00:00:00 2001 From: Chad Rockey Date: Thu, 11 Apr 2024 03:25:27 -0500 Subject: [PATCH 2/2] C# Pin buffer memory before taking unsafe pointer --- src/bindings/csharp/Socket.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/bindings/csharp/Socket.cs b/src/bindings/csharp/Socket.cs index 57354b41..c9608670 100644 --- a/src/bindings/csharp/Socket.cs +++ b/src/bindings/csharp/Socket.cs @@ -317,6 +317,10 @@ public Int32 Send(Byte[] buffer, int offset, int size, SocketFlags socketFlags) throw new ArgumentOutOfRangeException("offset"); } int flags = 0; + + // Must pin memory before using raw pointer + // C# garbage collector can move memory while in use + GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); IntPtr bufferPtr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0); return zts_bsd_send(_fd, bufferPtr + offset, (uint)size, (int)flags); } @@ -351,6 +355,9 @@ public Int32 Receive(byte[] buffer, int offset, int size, SocketFlags socketFlag throw new ArgumentOutOfRangeException("offset"); } int flags = 0; + // Must pin memory before using raw pointer + // C# garbage collector can move memory while in use + GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); IntPtr bufferPtr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0); return zts_bsd_recv(_fd, bufferPtr + offset, (uint)Buffer.ByteLength(buffer), (int)flags); }