Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

send byte[4096] via TcpClient will raise an ArgumentOutOfRangeException #11

Description

@rexxiang

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);
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions