Bug 1523818 - Don't pass null to nsDependentString() to fix crash in nsMapiHook::PopulateCompFieldsW. r+a=jorgk
authorKai Engert <kaie@kuix.de>
Thu, 31 Jan 2019 07:23:02 +0100
changeset 34315 9abb72db409cf02aeb51bd2ab787ee026d77afb9
parent 34314 b5802d8134e455897eac08e7750f2139ef3335aa
child 34316 fb7f15d135af1492727d944176ea765ae7e6157b
push id389
push userclokep@gmail.com
push dateMon, 18 Mar 2019 19:01:53 +0000
bugs1523818
Bug 1523818 - Don't pass null to nsDependentString() to fix crash in nsMapiHook::PopulateCompFieldsW. r+a=jorgk
mailnews/mapi/mapihook/src/msgMapiHook.cpp
mailnews/mapi/mapihook/src/msgMapiImp.cpp
--- a/mailnews/mapi/mapihook/src/msgMapiHook.cpp
+++ b/mailnews/mapi/mapihook/src/msgMapiHook.cpp
@@ -571,20 +571,19 @@ nsresult nsMapiHook::HandleAttachmentsW(
   }
   return rv ;
 }
 
 // this is used to convert non Unicode data and then populate comp fields
 nsresult nsMapiHook::PopulateCompFieldsWithConversion(lpnsMapiMessage aMessage,
                                     nsIMsgCompFields * aCompFields)
 {
-  nsresult rv = NS_OK;
   bool isUTF8 = aMessage->ulReserved == CP_UTF8;
 
-  if (aMessage->lpOriginator)
+  if (aMessage->lpOriginator && aMessage->lpOriginator->lpszAddress)
   {
     nsAutoString From;
     if (!isUTF8)
         From.Append(NS_ConvertASCIItoUTF16(aMessage->lpOriginator->lpszAddress));
     else
         From.Append(NS_ConvertUTF8toUTF16(aMessage->lpOriginator->lpszAddress));
     aCompFields->SetFrom (From);
   }
@@ -640,16 +639,17 @@ nsresult nsMapiHook::PopulateCompFieldsW
   // set To, Cc, Bcc
   aCompFields->SetTo (To) ;
   aCompFields->SetCc (Cc) ;
   aCompFields->SetBcc (Bcc) ;
 
   MOZ_LOG(MAPI, mozilla::LogLevel::Debug, ("to: %s cc: %s bcc: %s \n", NS_ConvertUTF16toUTF8(To).get(), NS_ConvertUTF16toUTF8(Cc).get(), NS_ConvertUTF16toUTF8(Bcc).get()));
 
   // set subject
+  nsresult rv = NS_OK;
   if (aMessage->lpszSubject)
   {
     nsAutoString Subject ;
     if (!isUTF8)
         rv = NS_CopyNativeToUnicode(nsDependentCString(aMessage->lpszSubject),
                                     Subject);
     else
         Subject.Append(NS_ConvertUTF8toUTF16(aMessage->lpszSubject));
@@ -691,19 +691,17 @@ nsresult nsMapiHook::PopulateCompFieldsW
 
   return rv ;
 }
 
 // This is used to populate comp fields with UTF-16 data from MAPISendMailW function.
 nsresult nsMapiHook::PopulateCompFieldsW(lpnsMapiMessageW aMessage,
                                          nsIMsgCompFields* aCompFields)
 {
-  nsresult rv = NS_OK;
-
-  if (aMessage->lpOriginator)
+  if (aMessage->lpOriginator && aMessage->lpOriginator->lpszAddress)
     aCompFields->SetFrom(nsDependentString(aMessage->lpOriginator->lpszAddress));
 
   nsAutoString To;
   nsAutoString Cc;
   nsAutoString Bcc;
 
   NS_NAMED_LITERAL_STRING(Comma, ",");
 
@@ -750,17 +748,17 @@ nsresult nsMapiHook::PopulateCompFieldsW
   aCompFields->SetCc(Cc);
   aCompFields->SetBcc(Bcc);
 
   // Set subject.
   if (aMessage->lpszSubject)
     aCompFields->SetSubject(nsDependentString(aMessage->lpszSubject));
 
   // handle attachments as File URL
-  rv = HandleAttachmentsW(aCompFields, aMessage->nFileCount, aMessage->lpFiles);
+  nsresult rv = HandleAttachmentsW(aCompFields, aMessage->nFileCount, aMessage->lpFiles);
   if (NS_FAILED(rv)) return rv;
 
   // Set body.
   if (aMessage->lpszNoteText)
   {
     nsString Body(aMessage->lpszNoteText);
     if (Body.IsEmpty() || Body.Last() != '\n')
       Body.AppendLiteral(CRLF);
--- a/mailnews/mapi/mapihook/src/msgMapiImp.cpp
+++ b/mailnews/mapi/mapihook/src/msgMapiImp.cpp
@@ -197,37 +197,30 @@ STDMETHODIMP CMapiImp::Login(unsigned lo
     }
 
     return S_OK;
 }
 
 STDMETHODIMP CMapiImp::SendMail( unsigned long aSession, lpnsMapiMessage aMessage,
      unsigned long aFlags, unsigned long aReserved)
 {
-    nsresult rv = NS_OK ;
-
-    MOZ_LOG(MAPI, mozilla::LogLevel::Debug, ("CMapiImp::SendMail using flags %d\n", aFlags));
-
-    // Handle possible nullptr argument.
-    nsMapiMessage Message;
-    memset(&Message, 0, sizeof(nsMapiMessage));
-
-    if (!aMessage)
-    {
-        aMessage = &Message;
-    }
-
-    MOZ_LOG(MAPI, mozilla::LogLevel::Debug, ("CMapiImp::SendMail flags=%x subject: %s sender: %s\n",
-      aFlags, (char *) aMessage->lpszSubject, (aMessage->lpOriginator) ? aMessage->lpOriginator->lpszAddress : ""));
+    MOZ_LOG(MAPI, mozilla::LogLevel::Debug,
+      ("CMapiImp::SendMail flags=%lx subject: %s sender: %s\n",
+       aFlags,
+       (aMessage && aMessage->lpszSubject) ? aMessage->lpszSubject : "(no subject)",
+       (aMessage && aMessage->lpOriginator && aMessage->lpOriginator->lpszAddress) ?
+          aMessage->lpOriginator->lpszAddress : "(no sender)"));
 
     /** create nsIMsgCompFields obj and populate it **/
+    nsresult rv = NS_OK ;
     nsCOMPtr<nsIMsgCompFields> pCompFields = do_CreateInstance(NS_MSGCOMPFIELDS_CONTRACTID, &rv) ;
     if (NS_FAILED(rv) || (!pCompFields) ) return MAPI_E_INSUFFICIENT_MEMORY ;
 
-    rv = nsMapiHook::PopulateCompFieldsWithConversion(aMessage, pCompFields);
+    if (aMessage)
+      rv = nsMapiHook::PopulateCompFieldsWithConversion(aMessage, pCompFields);
 
     if (NS_SUCCEEDED (rv))
     {
         // see flag to see if UI needs to be brought up
         if (!(aFlags & MAPI_DIALOG))
         {
             rv = nsMapiHook::BlindSendMail(aSession, pCompFields);
         }
@@ -238,35 +231,31 @@ STDMETHODIMP CMapiImp::SendMail( unsigne
     }
 
     return nsMAPIConfiguration::GetMAPIErrorFromNSError (rv) ;
 }
 
 STDMETHODIMP CMapiImp::SendMailW(unsigned long aSession, lpnsMapiMessageW aMessage,
                                  unsigned long aFlags, unsigned long aReserved)
 {
-    nsresult rv = NS_OK;
-
-    MOZ_LOG(MAPI, mozilla::LogLevel::Debug, ("CMapiImp::SendMailW using flags %d\n", aFlags));
-
-    // Handle possible nullptr argument.
-    nsMapiMessageW Message{};
-    if (!aMessage)
-      aMessage = &Message;
-
-    MOZ_LOG(MAPI, mozilla::LogLevel::Debug, ("CMapiImp::SendMailW flags=%x subject: %s sender: %s\n",
-            aFlags,
-            NS_ConvertUTF16toUTF8(aMessage->lpszSubject).get(),
-            NS_ConvertUTF16toUTF8((aMessage->lpOriginator) ? aMessage->lpOriginator->lpszAddress : L"").get()));
+    MOZ_LOG(MAPI, mozilla::LogLevel::Debug,
+      ("CMapiImp::SendMailW flags=%lx subject: %s sender: %s\n",
+       aFlags,
+       (aMessage && aMessage->lpszSubject) ?
+          NS_ConvertUTF16toUTF8(aMessage->lpszSubject).get() : "(no subject)",
+       (aMessage && aMessage->lpOriginator && aMessage->lpOriginator->lpszAddress) ?
+          NS_ConvertUTF16toUTF8(aMessage->lpOriginator->lpszAddress).get() : "(no sender)"));
 
     // Create nsIMsgCompFields obj and populate it.
+    nsresult rv = NS_OK;
     nsCOMPtr<nsIMsgCompFields> pCompFields = do_CreateInstance(NS_MSGCOMPFIELDS_CONTRACTID, &rv);
     if (NS_FAILED(rv) || !pCompFields) return MAPI_E_INSUFFICIENT_MEMORY;
 
-    rv = nsMapiHook::PopulateCompFieldsW(aMessage, pCompFields);
+    if (aMessage)
+      rv = nsMapiHook::PopulateCompFieldsW(aMessage, pCompFields);
 
     if (NS_SUCCEEDED (rv))
     {
       // Check flag to see if UI needs to be brought up.
       if (!(aFlags & MAPI_DIALOG))
       {
         rv = nsMapiHook::BlindSendMail(aSession, pCompFields);
       }