Bug 1597891 - Remove superfluous infinite loop in MimeObject ResetChannelCharset(). r=jorgk
☠☠ backed out by 9c5ad9180e91 ☠ ☠
authorBen Campbell <benc@thunderbird.net>
Mon, 02 Dec 2019 12:38:55 +0200
changeset 28298 fc1c18cda24b0a157d4b6d71c401bf4b5146b17e
parent 28297 f83cf1dd1132b33fe33787534097fd088fcccb6f
child 28299 9c5ad9180e9133d9268073251fe2a574cefd7716
push id16751
push usermkmelin@iki.fi
push dateMon, 02 Dec 2019 10:39:45 +0000
treeherdercomm-central@fc1c18cda24b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjorgk
bugs1597891
Bug 1597891 - Remove superfluous infinite loop in MimeObject ResetChannelCharset(). r=jorgk
mailnews/mime/src/mimemoz2.cpp
--- a/mailnews/mime/src/mimemoz2.cpp
+++ b/mailnews/mime/src/mimemoz2.cpp
@@ -1767,52 +1767,41 @@ extern "C" char *MimeGetStringByName(con
   }
 
   return strdup("???");
 }
 
 void ResetChannelCharset(MimeObject *obj) {
   if (obj->options && obj->options->stream_closure &&
       obj->options->default_charset && obj->headers) {
+    // The previous version of this code would have entered an infinite
+    // loop. But it never showed up, so it's not clear that it is ever called.
+    // See bug #1597891.
+    MOZ_DIAGNOSTIC_ASSERT(false, "Should never get here (see bug 1597891)");
+
     mime_stream_data *msd = (mime_stream_data *)(obj->options->stream_closure);
     char *ct = MimeHeaders_get(obj->headers, HEADER_CONTENT_TYPE, false, false);
-    if ((ct) && (msd) && (msd->channel)) {
-      char *ptr = strstr(ct, "charset=");
-      if (ptr) {
-        // First, setup the channel!
+    if (ct && msd && msd->channel) {
+      char *cSet = MimeHeaders_get_parameter(ct, "charset", nullptr, nullptr);
+      if (cSet) {
+        // First, setup the channel.
         msd->channel->SetContentType(nsDependentCString(ct));
 
         // Second, if this is a Save As operation, then we need to convert
-        // to override the output charset!
-        mime_stream_data *msd = GetMSD(obj->options);
-        if ((msd) && (msd->format_out == nsMimeOutput::nsMimeMessageSaveAs)) {
-          // Extract the charset alone
-          char *cSet = nullptr;
-          if (*(ptr + 8) == '"')
-            cSet = strdup(ptr + 9);
-          else
-            cSet = strdup(ptr + 8);
-          if (cSet) {
-            char *ptr2 = cSet;
-            while ((*cSet) && (*cSet != ' ') && (*cSet != ';') &&
-                   (*cSet != '\r') && (*cSet != '\n') && (*cSet != '"'))
-              ptr2++;
-
-            if (*cSet) {
-              PR_FREEIF(obj->options->default_charset);
-              obj->options->default_charset = strdup(cSet);
-              obj->options->override_charset = true;
-            }
-
-            PR_FREEIF(cSet);
-          }
+        // to override the output charset.
+        if (msd->format_out == nsMimeOutput::nsMimeMessageSaveAs) {
+          PR_FREEIF(obj->options->default_charset);
+          obj->options->default_charset = cSet;
+          cSet = nullptr;  // Ownership was transferred.
+          obj->options->override_charset = true;
         }
+        PR_FREEIF(cSet);
       }
-      PR_FREEIF(ct);
     }
+    PR_FREEIF(ct);
   }
 }
 
 ////////////////////////////////////////////////////////////
 // Function to get up mail/news fontlang
 ////////////////////////////////////////////////////////////
 
 nsresult GetMailNewsFont(MimeObject *obj, bool styleFixed,