Bug 1487421 - Don't clear leftovers when processing empty lines in Base64 encoded MIME-parts. r+a=jorgk
authorAlfred Peters <infofrommozilla@gmail.com>
Sun, 02 Sep 2018 15:45:00 +0200
changeset 31863 db740f0083da6a05819f4da327f6820122807dae
parent 31862 24735ab7b938e75fbaace5f9b8f69759463093ff
child 31864 0b31df771530ea6a6a9cc52fad3aeece89f9a491
push id86
push usermozilla@jorgk.com
push dateSun, 14 Oct 2018 09:47:49 +0000
treeherdercomm-esr60@e524897f5bde [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs1487421
Bug 1487421 - Don't clear leftovers when processing empty lines in Base64 encoded MIME-parts. r+a=jorgk
mailnews/mime/src/mimeenc.cpp
--- a/mailnews/mime/src/mimeenc.cpp
+++ b/mailnews/mime/src/mimeenc.cpp
@@ -215,16 +215,23 @@ mime_decode_base64_token (const char *in
   }
 }
 
 
 static int
 mime_decode_base64_buffer (MimeDecoderData *data,
                const char *buffer, int32_t length, int32_t *outSize)
 {
+  if (outSize)
+    *outSize = 0;
+
+  // Without input there is nothing to do.
+  if (length == 0)
+    return 1;
+
   /* Warning, we are overwriting the buffer which was passed in.
    This is ok, because decoding these formats will never result
    in larger data than the input, only smaller. */
   const char *in  = buffer;
   char *out = (char *) buffer;
   char token [4];
   int i;
   bool leftover = (data->token_size > 0);
@@ -268,19 +275,21 @@ mime_decode_base64_buffer (MimeDecoderDa
       /* If there are characters left over from the last time around,
        we might not have space in the buffer to do our dirty work
        (if there were 2 or 3 left over, then there is only room for
        1 or 2 in the buffer right now, and we need 3.)  This is only
        a problem for the first chunk in each buffer, so in that
        case, just write prematurely. */
       int n;
       n = mime_decode_base64_token (token, token);
-      n = data->write_buffer (token, n, data->closure);
+      if (outSize)
+        *outSize += n;
+      n = data->write_buffer(token, n, data->closure);
       if (n < 0) /* abort */
-      return n;
+        return n;
 
       /* increment buffer so that we don't write the 1 or 2 unused
        characters now at the front. */
       buffer = in;
       out = (char *) buffer;
 
       leftover = false;
     }
@@ -288,22 +297,22 @@ mime_decode_base64_buffer (MimeDecoderDa
     {
       int n = mime_decode_base64_token (token, out);
       /* Advance "out" by the number of bytes just written to it. */
       out += n;
     }
   }
 
   if (outSize)
-    *outSize = out - buffer;
+    *outSize += out - buffer;
   /* Now that we've altered the data in place, write it. */
   if (out > buffer)
-  return data->write_buffer (buffer, (out - buffer), data->closure);
+    return data->write_buffer(buffer, (out - buffer), data->closure);
   else
-  return 1;
+    return 1;
 }
 
 
 static int
 mime_decode_uue_buffer (MimeDecoderData *data,
             const char *input_buffer, int32_t input_length, int32_t *outSize)
 {
   /* First, copy input_buffer into state->line_buffer until we have