Bug 1597891 - Remove superfluous infinite loop in MimeObject ResetChannelCharset(). r=mkmelin,jorgk DONTBUILD
authorBen Campbell <benc@thunderbird.net>
Tue, 21 Jan 2020 12:35:15 +0200
changeset 37986 c5d3c34a34c9065caf1bad63dc0239f0dd71868f
parent 37985 c1457ba6362cbc7a1cbd1318ea449c99c9a5f697
child 37987 61106214bab978442d9db6b0d4d3a3e8969bf387
push id398
push userclokep@gmail.com
push dateMon, 09 Mar 2020 19:10:28 +0000
reviewersmkmelin, jorgk
bugs1597891
Bug 1597891 - Remove superfluous infinite loop in MimeObject ResetChannelCharset(). r=mkmelin,jorgk DONTBUILD
mailnews/mime/src/mimemoz2.cpp
--- a/mailnews/mime/src/mimemoz2.cpp
+++ b/mailnews/mime/src/mimemoz2.cpp
@@ -1769,50 +1769,39 @@ extern "C" char *MimeGetStringByName(con
   return strdup("???");
 }
 
 void ResetChannelCharset(MimeObject *obj) {
   if (obj->options && obj->options->stream_closure &&
       obj->options->default_charset && obj->headers) {
     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) {
+        // The content-type does specify a charset. 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) {
+          // The previous version of this code would have entered an infinite
+          // loop. But it never showed up, so it's not clear that we ever get
+          // here...  See bug #1597891.
+          PR_FREEIF(obj->options->default_charset);
+          obj->options->default_charset = cSet;
+          cSet = nullptr;  // Ownership was transferred.
+          obj->options->override_charset = true;
+          MOZ_DIAGNOSTIC_ASSERT(
+              false, "Ahh. So this code _is_ run after all! (see bug 1597891)");
         }
+        PR_FREEIF(cSet);
       }
-      PR_FREEIF(ct);
     }
+    PR_FREEIF(ct);
   }
 }
 
 ////////////////////////////////////////////////////////////
 // Function to get up mail/news fontlang
 ////////////////////////////////////////////////////////////
 
 nsresult GetMailNewsFont(MimeObject *obj, bool styleFixed,