Bug 1510028 - Add null check to avoid crash due to nsDependentCString(null). r=mkmelin a=jorgk
authorJorg K <jorgk@jorgk.com>
Tue, 27 Nov 2018 23:10:19 +0100
changeset 33515 c839e782792f00b76b94fa6004745d9e2006321e
parent 33514 b2a3f7b11639f5b4ea32f7f9d7b12ebd15e18e92
child 33516 ae1110c30db5c25634cd81ff2db544505dc75ce0
push id387
push userclokep@gmail.com
push dateMon, 10 Dec 2018 21:30:47 +0000
reviewersmkmelin, jorgk
bugs1510028
Bug 1510028 - Add null check to avoid crash due to nsDependentCString(null). r=mkmelin a=jorgk
mailnews/base/util/nsMsgI18N.cpp
mailnews/compose/src/nsMsgCompose.cpp
mailnews/import/outlook/src/MapiMessage.cpp
mailnews/import/outlook/src/nsOutlookCompose.cpp
mailnews/mime/src/mimedrft.cpp
mailnews/mime/src/mimehdrs.cpp
--- a/mailnews/base/util/nsMsgI18N.cpp
+++ b/mailnews/base/util/nsMsgI18N.cpp
@@ -161,19 +161,19 @@ void nsMsgI18NTextFileCharset(nsACString
   mozilla::dom::FallbackEncoding::FromLocale()->Name(aCharset);
 }
 
 // MIME encoder, output string should be freed by PR_FREE
 // XXX : fix callers later to avoid allocation and copy
 char * nsMsgI18NEncodeMimePartIIStr(const char *header, bool structured, const char *charset, int32_t fieldnamelen, bool usemime)
 {
   // No MIME, convert to the outgoing mail charset.
-  if (false == usemime) {
+  if (!usemime) {
     nsAutoCString convertedStr;
-    if (NS_SUCCEEDED(nsMsgI18NConvertFromUnicode(nsDependentCString(charset),
+    if (NS_SUCCEEDED(nsMsgI18NConvertFromUnicode(charset ? nsDependentCString(charset) : EmptyCString(),
                                                  NS_ConvertUTF8toUTF16(header),
                                                  convertedStr)))
       return PL_strdup(convertedStr.get());
     else
       return PL_strdup(header);
   }
 
   nsAutoCString encodedString;
--- a/mailnews/compose/src/nsMsgCompose.cpp
+++ b/mailnews/compose/src/nsMsgCompose.cpp
@@ -1290,25 +1290,24 @@ NS_IMETHODIMP nsMsgCompose::SendMsg(MSG_
     nsCOMPtr<nsPIDOMWindowOuter> window = nsPIDOMWindowOuter::From(m_window);
     window->GetPrompter(getter_AddRefs(prompt));
   }
 
   // Set content type based on which type of compose window we had.
   nsString contentType = (m_composeHTML) ? NS_LITERAL_STRING("text/html"):
                                            NS_LITERAL_STRING("text/plain");
   nsString msgBody;
+  const char *charset = m_compFields->GetCharacterSet();
   if (m_editor)
   {
     // Reset message body previously stored in the compose fields
     // There is 2 nsIMsgCompFields::SetBody() functions using a pointer as argument,
     // therefore a casting is required.
     m_compFields->SetBody((const char *)nullptr);
 
-    const char *charset = m_compFields->GetCharacterSet();
-
     uint32_t flags = nsIDocumentEncoder::OutputCRLineBreak |
                      nsIDocumentEncoder::OutputLFLineBreak;
 
     if (m_composeHTML) {
       flags |= nsIDocumentEncoder::OutputFormatted |
                nsIDocumentEncoder::OutputDisallowLineBreaking;
     } else {
       bool flowed, delsp, formatted, disallowBreaks;
@@ -1331,37 +1330,37 @@ NS_IMETHODIMP nsMsgCompose::SendMsg(MSG_
   {
     m_compFields->GetBody(msgBody);
   }
   if (!msgBody.IsEmpty())
   {
     bool isAsciiOnly = NS_IsAscii(static_cast<const char16_t*>(msgBody.get()));
     // Convert body to mail charset
     nsCString outCString;
-    rv = nsMsgI18NConvertFromUnicode(nsDependentCString(m_compFields->GetCharacterSet()),
+    rv = nsMsgI18NConvertFromUnicode(charset ? nsDependentCString(charset) : EmptyCString(),
                                      msgBody, outCString, true);
     if (m_compFields->GetForceMsgEncoding())
       isAsciiOnly = false;
     if (NS_SUCCEEDED(rv) && !outCString.IsEmpty())
     {
       // If the body contains characters outside the repertoire of the current
       // charset, just convert to UTF-8 and be done with it
       // unless disable_fallback_to_utf8 is set for this charset.
       if (NS_ERROR_UENC_NOMAPPING == rv)
       {
         bool needToCheckCharset;
         m_compFields->GetNeedToCheckCharset(&needToCheckCharset);
         if (needToCheckCharset)
         {
           bool disableFallback = false;
           nsCOMPtr<nsIPrefBranch> prefBranch (do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
-          if (prefBranch)
+          if (prefBranch && charset)
           {
             nsCString prefName("mailnews.disable_fallback_to_utf8.");
-            prefName.Append(m_compFields->GetCharacterSet());
+            prefName.Append(charset);
             prefBranch->GetBoolPref(prefName.get(), &disableFallback);
           }
           if (!disableFallback)
           {
             CopyUTF16toUTF8(msgBody, outCString);
             m_compFields->SetCharacterSet("UTF-8");
             SetDocumentCharset("UTF-8");
           }
--- a/mailnews/import/outlook/src/MapiMessage.cpp
+++ b/mailnews/import/outlook/src/MapiMessage.cpp
@@ -1268,19 +1268,21 @@ void CMapiMessageHeaders::CHeaderField::
     if ((*pos == nsCRT::CR) && (*(pos+1) == nsCRT::LF) && IsWSP(*(pos+2)))
       pos += 2; // Skip CRLF if it is followed by SPACE or TAB
     else
       unfolded.Append(*(pos++));
   }
   if (m_fbody_utf8)
     CopyUTF8toUTF16(unfolded, dest);
   else
-    nsMsgI18NConvertToUnicode(nsDependentCString(fallbackCharset),
-                                                 unfolded,
-                                                 dest);
+    nsMsgI18NConvertToUnicode(fallbackCharset ?
+                                nsDependentCString(fallbackCharset) :
+                                EmptyCString(),
+                              unfolded,
+                              dest);
 }
 
 ////////////////////////////////////////
 
 const char* CMapiMessageHeaders::Specials[hdrMax] = {
   "Date:",
   "From:",
   "Sender:",
--- a/mailnews/import/outlook/src/nsOutlookCompose.cpp
+++ b/mailnews/import/outlook/src/nsOutlookCompose.cpp
@@ -274,17 +274,18 @@ nsresult nsOutlookCompose::ComposeTheMes
           new nsImportEmbeddedImageData(uri, nsDependentCString(cid),
                                      nsDependentCString(name));
         embeddedObjects->AppendElement(imageData);
       }
     }
   }
 
   nsCString bodyA;
-  nsMsgI18NConvertFromUnicode(nsDependentCString(msg.GetBodyCharset()), bodyW, bodyA);
+  const char *charset = msg.GetBodyCharset();
+  nsMsgI18NConvertFromUnicode(charset ? nsDependentCString(charset) : EmptyCString(), bodyW, bodyA);
 
   nsCOMPtr<nsIImportService> impService(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // nsIImportService::CreateRFC822Message creates a runnable and dispatches to the main thread.
   rv = impService->CreateRFC822Message(
                         m_pIdentity,                  // dummy identity
                         m_pMsgFields,                 // message fields
--- a/mailnews/mime/src/mimedrft.cpp
+++ b/mailnews/mime/src/mimedrft.cpp
@@ -344,50 +344,50 @@ CreateCompositionFields(const char      
   // Now set all of the passed in stuff...
   cFields->SetCharacterSet(!PL_strcasecmp("us-ascii", charset) ? "ISO-8859-1" : charset);
 
   nsAutoCString val;
   nsAutoString outString;
 
   if (from) {
     nsMsgI18NConvertRawBytesToUTF16(nsDependentCString(from),
-                                    nsDependentCString(charset),
+                                    charset ? nsDependentCString(charset) : EmptyCString(),
                                     outString);
     cFields->SetFrom(outString);
   }
 
   if (subject) {
     MIME_DecodeMimeHeader(subject, charset, false, true, val);
     cFields->SetSubject(NS_ConvertUTF8toUTF16(!val.IsEmpty() ? val.get() : subject));
   }
 
   if (reply_to) {
     nsMsgI18NConvertRawBytesToUTF16(nsDependentCString(reply_to),
-                                    nsDependentCString(charset),
+                                    charset ? nsDependentCString(charset) : EmptyCString(),
                                     outString);
     cFields->SetReplyTo(outString);
   }
 
   if (to) {
     nsMsgI18NConvertRawBytesToUTF16(nsDependentCString(to),
-                                    nsDependentCString(charset),
+                                    charset ? nsDependentCString(charset) : EmptyCString(),
                                     outString);
     cFields->SetTo(outString);
   }
 
   if (cc) {
     nsMsgI18NConvertRawBytesToUTF16(nsDependentCString(cc),
-                                    nsDependentCString(charset),
+                                    charset ? nsDependentCString(charset) : EmptyCString(),
                                     outString);
     cFields->SetCc(outString);
   }
 
   if (bcc) {
     nsMsgI18NConvertRawBytesToUTF16(nsDependentCString(bcc),
-                                    nsDependentCString(charset),
+                                    charset ? nsDependentCString(charset) : EmptyCString(),
                                     outString);
     cFields->SetBcc(outString);
   }
 
   if (fcc) {
     MIME_DecodeMimeHeader(fcc, charset, false, true, val);
     cFields->SetFcc(NS_ConvertUTF8toUTF16(!val.IsEmpty() ? val.get() : fcc));
   }
--- a/mailnews/mime/src/mimehdrs.cpp
+++ b/mailnews/mime/src/mimehdrs.cpp
@@ -35,17 +35,19 @@ MimeHeaders_convert_header_value(MimeDis
 {
   if (value.IsEmpty())
     return;
 
   if (convert_charset_only)
   {
     nsAutoCString output;
     nsMsgI18NConvertRawBytesToUTF8(value,
-                                   nsDependentCString(opt->default_charset),
+                                   opt->default_charset ?
+                                     nsDependentCString(opt->default_charset) :
+                                     EmptyCString(),
                                    output);
     value.Assign(output);
     return;
   }
 
   if (opt && opt->rfc1522_conversion_p)
   {
     nsAutoCString temporary;