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
Fixes issue #1498
  • Loading branch information
jstedfast committed Jan 4, 2023
1 parent be54679 commit 6990619
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions MailKit/Net/Smtp/SmtpStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -592,14 +592,18 @@ public async Task<SmtpResponse> ReadResponseAsync (CancellationToken cancellatio
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 6990619

Please sign in to comment.