NetGain icon indicating copy to clipboard operation
NetGain copied to clipboard

send byte[4096] via TcpClient will raise an ArgumentOutOfRangeException

Open rexxiang opened this issue 9 years ago • 0 comments

The following code will reproduce the bug.

var endpoint = new IPEndPoint(IPAddress.Loopback, 6002);
using (var client = new TcpClient()) {
    client.ProtocolFactory = WebSocketClientFactory.Default;
    client.Open(endpoint);

    var dataLength = 4096;
    var bytes = new byte[dataLength];
    var resp = (string)client.ExecuteSync(bytes);

    WriteLine($"{DateTime.Now:HH:mm:ss fff}\t{resp}");
}

I think there must be something wrong in method: BufferStream.Write(byte[] buffer, int bufferOffset, int count).

int origCount = count;
CheckDisposed();
if(isReadOnly) throw new NotSupportedException();

int newEnd = checked((offset - origin) + count);
if (newEnd > length)
{
    Grow(newEnd);
    length = newEnd;
}

int chunkIndex = offset/NetContext.BufferSize, chunkOffet = offset%NetContext.BufferSize;
int thisPage = NetContext.BufferSize - chunkOffet;
var chunk = buffers[chunkIndex]; // will throw ArgumentOutOfRangeException if the buffer.Length == 4096
if (thisPage >= count)
{ 
    // can write to a single page
    Buffer.BlockCopy(buffer, bufferOffset, chunk, chunkOffet, count);
}

rexxiang avatar May 23 '16 08:05 rexxiang