Skip to content

Commit

Permalink
Calculate needed bytes before converting into the output buffer
Browse files Browse the repository at this point in the history
This fix is identical to commit 6990619

Updates the Pop3Stream to behave identical to the SmtpStream.
  • Loading branch information
jstedfast committed Apr 10, 2023
1 parent cf6c5e8 commit e303448
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions MailKit/Net/Pop3/Pop3Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -695,14 +695,18 @@ internal async Task<bool> ReadLineAsync (ByteArrayBuilder builder, CancellationT
unsafe bool TryQueueCommand (Encoder encoder, string command, ref int index)
{
fixed (char* cmd = command) {
int outputLeft = output.Length - outputIndex;
int charCount = command.Length - index;
char* chars = cmd + index;

var needed = encoder.GetByteCount (chars, charCount, true);
if (needed > outputLeft)
return false;

fixed (byte* outbuf = output) {
int byteCount = output.Length - outputIndex;
byte* outptr = outbuf + outputIndex;

encoder.Convert (chars, charCount, outptr, byteCount, true, out int charsUsed, out int bytesUsed, out bool completed);
encoder.Convert (chars, charCount, outptr, outputLeft, true, out int charsUsed, out int bytesUsed, out bool completed);
outputIndex += bytesUsed;
index += charsUsed;

Expand Down

0 comments on commit e303448

Please sign in to comment.