Skip to content

BaseChannel/GetBytes, buffer overrun potential #9

@mr23

Description

@mr23

This needs some improvement to not overrun, and would be more useful if the byte array size was configurable. Also, add BytesToRead to the SP implementation, and use here for performance; push the byte content parsing up. Something like:

public int BufferLen { get; set/* use a method to change buffer size, protected with a lock, or disallow setting when port is opened*/; }

// don't worry about byte values here, just buffer and return the input, parse in caller.
protected byte[] GetBytes(SerialPort port)
{
int position = 0;
int bytesWaiting = port.BytesToRead;
bytesWaiting = Min(bytesWaiting,BufferLen);
byte[] buffer = new byte[bytesWaiting];
var bytesWaiting = port.Read(buffer,0,bytesWaiting); // returns bytes read
return buffer;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions