foens / hpop

OpenPOP.NET code repository
http://hpop.sourceforge.net/
204 stars 114 forks source link

Control characters in message returned from server #67

Open BraveThrasher opened 6 years ago

BraveThrasher commented 6 years ago

It can happen that messages returned by the server start with control characters (non-printable characters) but after those control characters comes a valid message (starting with a '+') from the server. Those control characters should not be taken into account when checking a message for validity.

We get this on a very regular basis and getting an exception each time is very annoying. We handle exceptions by logging them and the log file gets swamped by the exceptions that are being thrown because of this.

The utility method ReadLineAsBytes in StreamUtility.cs should also include a check for control characters in the following piece of code.

// Do not write \r or \n
if (readChar != '\r' && readChar != '\n')
    memoryStream.WriteByte((byte)justRead);

It only needs to be altered as follows.

// Do not write \r or \n
if (readChar != '\r' && readChar != '\n' && !char.IsControl(readChar))
    memoryStream.WriteByte((byte)justRead);