Bug 413544 - Add null-arg checks in nsMsgMailSession.cpp, nsMimeBaseEmitter.cpp, nsMsgCompose.cpp and nsMsgAccount.cpp. r=standard8
authoraceman <acelists@atlas.sk>
Mon, 11 Jun 2012 19:13:20 -0400
changeset 12625 b7d246591716682452465248862a98aba9eb47dc
parent 12624 7447698d6473435b31c7f6185a856b8b65706ad2
child 12626 f7a0c097c8496694d4009d3bf34d6f7960680d6b
push idunknown
push userunknown
push dateunknown
reviewersstandard8
bugs413544
Bug 413544 - Add null-arg checks in nsMsgMailSession.cpp, nsMimeBaseEmitter.cpp, nsMsgCompose.cpp and nsMsgAccount.cpp. r=standard8
mailnews/base/src/nsMsgAccount.cpp
mailnews/base/src/nsMsgMailSession.cpp
mailnews/compose/src/nsMsgCompose.cpp
mailnews/mime/emitters/src/nsMimeBaseEmitter.cpp
--- a/mailnews/base/src/nsMsgAccount.cpp
+++ b/mailnews/base/src/nsMsgAccount.cpp
@@ -49,17 +49,17 @@ nsMsgAccount::getPrefService()
     return NS_OK;
 
   nsresult rv;
   m_prefs = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
   return rv;
 }
 
 NS_IMETHODIMP
-nsMsgAccount::GetIncomingServer(nsIMsgIncomingServer * *aIncomingServer)
+nsMsgAccount::GetIncomingServer(nsIMsgIncomingServer **aIncomingServer)
 {
   NS_ENSURE_ARG_POINTER(aIncomingServer);
 
   // create the incoming server lazily
   if (!m_incomingServer) {
     // ignore the error (and return null), but it's still bad so assert
     nsresult rv = createIncomingServer();
     NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "couldn't lazily create the server\n");
@@ -68,34 +68,33 @@ nsMsgAccount::GetIncomingServer(nsIMsgIn
   NS_IF_ADDREF(*aIncomingServer = m_incomingServer);
 
   return NS_OK;
 }
 
 nsresult
 nsMsgAccount::createIncomingServer()
 {
-  if (m_accountKey.IsEmpty())
-    return NS_ERROR_NOT_INITIALIZED;
+  NS_ENSURE_FALSE(m_accountKey.IsEmpty(), NS_ERROR_NOT_INITIALIZED);
 
   // from here, load mail.account.myaccount.server
   // Load the incoming server
   //
   // ex) mail.account.myaccount.server = "myserver"
 
   nsresult rv = getPrefService();
-  if (NS_FAILED(rv)) return rv;
+  NS_ENSURE_SUCCESS(rv, rv);
 
   // get the "server" pref
   nsCAutoString serverKeyPref("mail.account.");
   serverKeyPref += m_accountKey;
   serverKeyPref += ".server";
   nsCString serverKey;
   rv = m_prefs->GetCharPref(serverKeyPref.get(), getter_Copies(serverKey));
-  if (NS_FAILED(rv)) return rv;
+  NS_ENSURE_SUCCESS(rv, rv);
 
   // get the server from the account manager
   nsCOMPtr<nsIMsgAccountManager> accountManager =
            do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIMsgIncomingServer> server;
   rv = accountManager->GetIncomingServer(serverKey, getter_AddRefs(server));
@@ -105,18 +104,20 @@ nsMsgAccount::createIncomingServer()
   m_incomingServer = server;
   accountManager->NotifyServerLoaded(server);
 
   return NS_OK;
 }
 
 
 NS_IMETHODIMP
-nsMsgAccount::SetIncomingServer(nsIMsgIncomingServer * aIncomingServer)
+nsMsgAccount::SetIncomingServer(nsIMsgIncomingServer *aIncomingServer)
 {
+  NS_ENSURE_ARG_POINTER(aIncomingServer);
+
   nsCString key;
   nsresult rv = aIncomingServer->GetKey(key);
 
   if (NS_SUCCEEDED(rv)) {
     nsCAutoString serverPrefName("mail.account.");
     serverPrefName.Append(m_accountKey);
     serverPrefName.AppendLiteral(".server");
     m_prefs->SetCharPref(serverPrefName.get(), key.get());
@@ -182,19 +183,18 @@ nsMsgAccount::GetIdentities(nsISupportsA
 
 /*
  * set up the m_identities array
  * do not call this more than once or we'll leak.
  */
 nsresult
 nsMsgAccount::createIdentities()
 {
-  NS_ENSURE_TRUE(!m_accountKey.IsEmpty(), NS_ERROR_NOT_INITIALIZED);
-  if (m_identities)
-    return NS_ERROR_FAILURE;
+  NS_ENSURE_FALSE(m_accountKey.IsEmpty(), NS_ERROR_NOT_INITIALIZED);
+  NS_ENSURE_FALSE(m_identities, NS_ERROR_FAILURE);
 
   NS_NewISupportsArray(getter_AddRefs(m_identities));
 
   // get the pref
   // ex) mail.account.myaccount.identities = "joe-home,joe-work"
   nsCAutoString identitiesKeyPref("mail.account.");
   identitiesKeyPref.Append(m_accountKey);
   identitiesKeyPref.Append(".identities");
@@ -240,39 +240,40 @@ nsMsgAccount::createIdentities()
 }
 
 
 /* attribute nsIMsgIdentity defaultIdentity; */
 NS_IMETHODIMP
 nsMsgAccount::GetDefaultIdentity(nsIMsgIdentity **aDefaultIdentity)
 {
   NS_ENSURE_ARG_POINTER(aDefaultIdentity);
+
   *aDefaultIdentity = nsnull;
   nsresult rv;
-  
+
   if (!m_identities)
   {
     rv = Init();
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   PRUint32 count;
   rv = m_identities->Count(&count);
   NS_ENSURE_SUCCESS(rv, rv);
   if (count == 0)
     return NS_OK;
-  
+
   nsCOMPtr<nsIMsgIdentity> identity( do_QueryElementAt(m_identities, 0, &rv));
   identity.swap(*aDefaultIdentity);
   return rv;
 }
 
 // todo - make sure this is in the identity array!
 NS_IMETHODIMP
-nsMsgAccount::SetDefaultIdentity(nsIMsgIdentity * aDefaultIdentity)
+nsMsgAccount::SetDefaultIdentity(nsIMsgIdentity *aDefaultIdentity)
 {
   NS_ENSURE_TRUE(m_identities, NS_ERROR_FAILURE);
 
   NS_ASSERTION(m_identities->IndexOf(aDefaultIdentity) != -1, "Where did that identity come from?!");
   if (m_identities->IndexOf(aDefaultIdentity) == -1)
     return NS_ERROR_UNEXPECTED;
 
   m_defaultIdentity = aDefaultIdentity;
@@ -281,30 +282,31 @@ nsMsgAccount::SetDefaultIdentity(nsIMsgI
 
 // add the identity to m_identities, but don't fiddle with the
 // prefs. The assumption here is that the pref for this identity is
 // already set.
 nsresult
 nsMsgAccount::addIdentityInternal(nsIMsgIdentity *identity)
 {
   NS_ENSURE_TRUE(m_identities, NS_ERROR_FAILURE);
+
   return m_identities->AppendElement(identity);
 }
 
 /* void addIdentity (in nsIMsgIdentity identity); */
 NS_IMETHODIMP
 nsMsgAccount::AddIdentity(nsIMsgIdentity *identity)
 {
+  NS_ENSURE_ARG_POINTER(identity);
+
   // hack hack - need to add this to the list of identities.
   // for now just treat this as a Setxxx accessor
   // when this is actually implemented, don't refcount the default identity
-  nsresult rv;
-
   nsCString key;
-  rv = identity->GetKey(key);
+  nsresult rv = identity->GetKey(key);
 
   if (NS_SUCCEEDED(rv)) {
 
     nsCAutoString identitiesKeyPref("mail.account.");
     identitiesKeyPref.Append(m_accountKey);
     identitiesKeyPref.Append(".identities");
 
     nsCString identityList;
@@ -351,22 +353,22 @@ nsMsgAccount::AddIdentity(nsIMsgIdentity
   if (!m_defaultIdentity)
     SetDefaultIdentity(identity);
 
   return rv;
 }
 
 /* void removeIdentity (in nsIMsgIdentity identity); */
 NS_IMETHODIMP
-nsMsgAccount::RemoveIdentity(nsIMsgIdentity * aIdentity)
+nsMsgAccount::RemoveIdentity(nsIMsgIdentity *aIdentity)
 {
   NS_ENSURE_ARG_POINTER(aIdentity);
   NS_ENSURE_TRUE(m_identities, NS_ERROR_FAILURE);
 
-  PRUint32 count =0;
+  PRUint32 count = 0;
   m_identities->Count(&count);
 
   NS_ENSURE_TRUE(count > 1, NS_ERROR_FAILURE); // you must have at least one identity
 
   nsCString key;
   nsresult rv = aIdentity->GetKey(key);
 
   // remove our identity
@@ -434,22 +436,21 @@ nsMsgAccount::ToString(nsAString& aResul
   aResult.Append(NS_ConvertASCIItoUTF16(m_accountKey));
   aResult.AppendLiteral("]");
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsMsgAccount::ClearAllValues()
 {
-  nsresult rv;
   nsCAutoString rootPref("mail.account.");
   rootPref += m_accountKey;
   rootPref += '.';
 
-  rv = getPrefService();
+  nsresult rv = getPrefService();
   NS_ENSURE_SUCCESS(rv, rv);
 
   PRUint32 cntChild, i;
   char **childArray;
 
   rv = m_prefs->GetChildList(rootPref.get(), &cntChild, &childArray);
   if (NS_SUCCEEDED(rv)) 
   {
--- a/mailnews/base/src/nsMsgMailSession.cpp
+++ b/mailnews/base/src/nsMsgMailSession.cpp
@@ -165,17 +165,16 @@ NS_IMETHODIMP nsMsgMailSession::OnItemAd
 
 NS_IMETHODIMP nsMsgMailSession::OnItemRemoved(nsIMsgFolder *aParentItem,
                                               nsISupports *aItem)
 {
   NOTIFY_FOLDER_LISTENERS(removed, OnItemRemoved, (aParentItem, aItem));
   return NS_OK;
 }
 
-
 NS_IMETHODIMP nsMsgMailSession::OnItemEvent(nsIMsgFolder *aFolder,
                                             nsIAtom *aEvent)
 {
   NOTIFY_FOLDER_LISTENERS(event, OnItemEvent, (aFolder, aEvent));
   return NS_OK;
 }
 
 NS_IMETHODIMP
@@ -247,22 +246,22 @@ nsMsgMailSession::AlertUser(const nsAStr
   }
 
   if (dialog)
     return dialog->Alert(nsnull, PromiseFlatString(aMessage).get());
 
   return NS_OK;
 }
 
-nsresult nsMsgMailSession::GetTopmostMsgWindow(nsIMsgWindow* *aMsgWindow)
+nsresult nsMsgMailSession::GetTopmostMsgWindow(nsIMsgWindow **aMsgWindow)
 {
   NS_ENSURE_ARG_POINTER(aMsgWindow);
-  
+
   *aMsgWindow = nsnull;
- 
+
   PRUint32 count = mWindows.Count();
 
   if (count == 1)
   {
     NS_ADDREF(*aMsgWindow = mWindows[0]);
     return (*aMsgWindow) ? NS_OK : NS_ERROR_FAILURE;
   }
   else if (count > 1)
@@ -349,21 +348,20 @@ nsresult nsMsgMailSession::GetTopmostMsg
         }
       }
     }
   }
 
   return (*aMsgWindow) ? NS_OK : NS_ERROR_FAILURE;
 }
 
-
-
 NS_IMETHODIMP nsMsgMailSession::AddMsgWindow(nsIMsgWindow *msgWindow)
 {
   NS_ENSURE_ARG_POINTER(msgWindow);
+
   mWindows.AppendObject(msgWindow);
   return NS_OK;
 }
 
 NS_IMETHODIMP nsMsgMailSession::RemoveMsgWindow(nsIMsgWindow *msgWindow)
 {
   mWindows.RemoveObject(msgWindow);
   // Mac keeps a hidden window open so the app doesn't shut down when
@@ -381,128 +379,126 @@ NS_IMETHODIMP nsMsgMailSession::RemoveMs
     accountManager->CleanupOnExit();
   }
 #endif
   return NS_OK;
 }
 
 NS_IMETHODIMP nsMsgMailSession::IsFolderOpenInWindow(nsIMsgFolder *folder, bool *aResult)
 {
-  if (!aResult)
-    return NS_ERROR_NULL_POINTER;
+  NS_ENSURE_ARG_POINTER(aResult);
+
   *aResult = false;
-  
+
   PRUint32 count = mWindows.Count();
-  
+
   for(PRUint32 i = 0; i < count; i++)
   {
     nsCOMPtr<nsIMsgFolder> openFolder;
     mWindows[i]->GetOpenFolder(getter_AddRefs(openFolder));
     if (folder == openFolder.get())
     {
       *aResult = true;
       break;
     }
   }
-  
+
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsMsgMailSession::ConvertMsgURIToMsgURL(const char *aURI, nsIMsgWindow *aMsgWindow, char **aURL)
 {
-  if ((!aURI) || (!aURL))
-    return NS_ERROR_NULL_POINTER;
+  NS_ENSURE_ARG_POINTER(aURI);
+  NS_ENSURE_ARG_POINTER(aURL);
 
   // convert the rdf msg uri into a url that represents the message...
   nsCOMPtr <nsIMsgMessageService> msgService;
   nsresult rv = GetMessageServiceFromURI(nsDependentCString(aURI), getter_AddRefs(msgService));
-  if (NS_FAILED(rv)) 
-    return NS_ERROR_NULL_POINTER;
+  NS_ENSURE_SUCCESS(rv, NS_ERROR_NULL_POINTER);
 
   nsCOMPtr<nsIURI> tURI;
   rv = msgService->GetUrlForUri(aURI, getter_AddRefs(tURI), aMsgWindow);
-  if (NS_FAILED(rv)) 
-    return NS_ERROR_NULL_POINTER;
+  NS_ENSURE_SUCCESS(rv, NS_ERROR_NULL_POINTER);
 
   nsCAutoString urlString;
   if (NS_SUCCEEDED(tURI->GetSpec(urlString)))
   {
     *aURL = ToNewCString(urlString);
-    if (!(aURL))
-      return NS_ERROR_NULL_POINTER;
+    NS_ENSURE_ARG_POINTER(aURL);
   }
   return rv;
 }
 
 //----------------------------------------------------------------------------------------
 // GetSelectedLocaleDataDir - If a locale is selected, appends the selected locale to the
 //                            defaults data dir and returns that new defaults data dir
 //----------------------------------------------------------------------------------------
 nsresult
 nsMsgMailSession::GetSelectedLocaleDataDir(nsIFile *defaultsDir)
-{                                                                               
-  NS_ENSURE_ARG_POINTER(defaultsDir);                                     
+{
+  NS_ENSURE_ARG_POINTER(defaultsDir);
 
-  nsresult rv;                                                                
-  bool baseDirExists = false;                                            
-  rv = defaultsDir->Exists(&baseDirExists);                               
-  NS_ENSURE_SUCCESS(rv,rv);                                                   
+  bool baseDirExists = false;
+  nsresult rv = defaultsDir->Exists(&baseDirExists);
+  NS_ENSURE_SUCCESS(rv, rv);
 
-  if (baseDirExists) {                                                        
+  if (baseDirExists) {
     nsCOMPtr<nsIXULChromeRegistry> packageRegistry =
       mozilla::services::GetXULChromeRegistryService();
-    if (packageRegistry) {                                                 
-      nsCAutoString localeName;                                           
+    if (packageRegistry) {
+      nsCAutoString localeName;
       rv = packageRegistry->GetSelectedLocale(NS_LITERAL_CSTRING("global-region"), localeName);
 
       if (NS_SUCCEEDED(rv) && !localeName.IsEmpty()) {
-        bool localeDirExists = false;                              
-        nsCOMPtr<nsIFile> localeDataDir;                                
-        
-        rv = defaultsDir->Clone(getter_AddRefs(localeDataDir));     
-        NS_ENSURE_SUCCESS(rv,rv);                                       
+        bool localeDirExists = false;
+        nsCOMPtr<nsIFile> localeDataDir;
+
+        rv = defaultsDir->Clone(getter_AddRefs(localeDataDir));
+        NS_ENSURE_SUCCESS(rv, rv);
 
         rv = localeDataDir->AppendNative(localeName);
-        NS_ENSURE_SUCCESS(rv,rv);                                       
+        NS_ENSURE_SUCCESS(rv, rv);
 
-        rv = localeDataDir->Exists(&localeDirExists);                   
-        NS_ENSURE_SUCCESS(rv,rv);                                       
+        rv = localeDataDir->Exists(&localeDirExists);
+        NS_ENSURE_SUCCESS(rv, rv);
 
-        if (localeDirExists) {                                          
-          // use locale provider instead                              
+        if (localeDirExists) {
+          // use locale provider instead
           rv = defaultsDir->AppendNative(localeName);
-          NS_ENSURE_SUCCESS(rv,rv);                                   
-        }                                                               
-      }                                                                   
-    }                                                                       
-  }                                                                           
-  return NS_OK;                                                               
-} 
+          NS_ENSURE_SUCCESS(rv, rv);
+        }
+      }
+    }
+  }
+  return NS_OK;
+}
 
 //----------------------------------------------------------------------------------------
-// GetDataFilesDir - Gets the application's default folder and then appends the 
-//                   subdirectory named passed in as param dirName. If there is a seleccted
+// GetDataFilesDir - Gets the application's default folder and then appends the
+//                   subdirectory named passed in as param dirName. If there is a selected
 //                   locale, will append that to the dir path before returning the value
 //----------------------------------------------------------------------------------------
 NS_IMETHODIMP
 nsMsgMailSession::GetDataFilesDir(const char* dirName, nsIFile **dataFilesDir)
-{                                                                                                                                                    
+{
+
+  NS_ENSURE_ARG_POINTER(dirName);
   NS_ENSURE_ARG_POINTER(dataFilesDir);
 
   nsresult rv;
-  nsCOMPtr<nsIProperties> directoryService = 
+  nsCOMPtr<nsIProperties> directoryService =
     do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv,rv);
+  NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIFile> defaultsDir;
-  rv = directoryService->Get(NS_APP_DEFAULTS_50_DIR, 
-                             NS_GET_IID(nsIFile), 
+  rv = directoryService->Get(NS_APP_DEFAULTS_50_DIR,
+                             NS_GET_IID(nsIFile),
                              getter_AddRefs(defaultsDir));
-  NS_ENSURE_SUCCESS(rv,rv);
+  NS_ENSURE_SUCCESS(rv, rv);
 
   rv = defaultsDir->AppendNative(nsDependentCString(dirName));
   if (NS_SUCCEEDED(rv))
     rv = GetSelectedLocaleDataDir(defaultsDir);
 
   NS_IF_ADDREF(*dataFilesDir = defaultsDir);
 
   return rv;
@@ -538,27 +534,27 @@ nsMsgShutdownService::~nsMsgShutdownServ
     observerService->RemoveObserver(this, "quit-application-granted");
     observerService->RemoveObserver(this, "quit-application");
   }
 }
 
 nsresult nsMsgShutdownService::ProcessNextTask()
 {
   bool shutdownTasksDone = true;
-  
+
   PRInt32 count = mShutdownTasks.Count();
   if (mTaskIndex < count)
   {
     shutdownTasksDone = false;
 
-    nsCOMPtr<nsIMsgShutdownTask> curTask = mShutdownTasks[mTaskIndex];    
+    nsCOMPtr<nsIMsgShutdownTask> curTask = mShutdownTasks[mTaskIndex];
     nsString taskName;
     curTask->GetCurrentTaskName(taskName); 
     SetStatusText(taskName);
-   
+
     nsCOMPtr<nsIMsgMailSession> mailSession = do_GetService(NS_MSGMAILSESSION_CONTRACTID);
     NS_ENSURE_TRUE(mailSession, NS_ERROR_FAILURE);
 
     nsCOMPtr<nsIMsgWindow> topMsgWindow;
     mailSession->GetTopmostMsgWindow(getter_AddRefs(topMsgWindow));
 
     bool taskIsRunning = true;
     nsresult rv = curTask->DoShutdownTask(this, topMsgWindow, &taskIsRunning);
@@ -568,20 +564,21 @@ nsresult nsMsgShutdownService::ProcessNe
       mTaskIndex++;
       mMsgProgress->OnProgressChange(nsnull, nsnull, 0, 0, mTaskIndex, count);
       ProcessNextTask();
     }
   }
 
   if (shutdownTasksDone)
   {
-    mMsgProgress->OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP, NS_OK);
+    if (mMsgProgress)
+      mMsgProgress->OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP, NS_OK);
     AttemptShutdown();
   }
-  
+
   return NS_OK;
 }
 
 void nsMsgShutdownService::AttemptShutdown()
 {
   if (mQuitForced)
   {
     PR_CEnterMonitor(this);
@@ -733,19 +730,22 @@ NS_IMETHODIMP nsMsgShutdownService::OnSt
 {
   return NS_OK;
 }
 
 NS_IMETHODIMP nsMsgShutdownService::OnStopRunningUrl(nsIURI *url, nsresult aExitCode)
 {
   mTaskIndex++;
 
-  PRInt32 numTasks = mShutdownTasks.Count();
-  mMsgProgress->OnProgressChange(nsnull, nsnull, 0, 0, mTaskIndex, numTasks);
-  
+  if (mMsgProgress)
+  {
+    PRInt32 numTasks = mShutdownTasks.Count();
+    mMsgProgress->OnProgressChange(nsnull, nsnull, 0, 0, mTaskIndex, numTasks);
+  }
+
   ProcessNextTask();
   return NS_OK;
 }
 
 NS_IMETHODIMP nsMsgShutdownService::GetNumTasks(PRInt32 *inNumTasks)
 {
   *inNumTasks = mShutdownTasks.Count();
   return NS_OK;
@@ -761,11 +761,12 @@ NS_IMETHODIMP nsMsgShutdownService::Canc
 {
   AttemptShutdown();
   return NS_OK;
 }
 
 NS_IMETHODIMP nsMsgShutdownService::SetStatusText(const nsAString & inStatusString)
 {
   nsString statusString(inStatusString);
-  mMsgProgress->OnStatusChange(nsnull, nsnull, NS_OK, statusString.get());
+  if (mMsgProgress)
+    mMsgProgress->OnStatusChange(nsnull, nsnull, NS_OK, nsString(statusString).get());
   return NS_OK;
 }
--- a/mailnews/compose/src/nsMsgCompose.cpp
+++ b/mailnews/compose/src/nsMsgCompose.cpp
@@ -979,16 +979,18 @@ nsMsgCompose::Initialize(nsIMsgComposePa
     rv = composeService->RegisterComposeDocShell(aDocShell, this);
     NS_ENSURE_SUCCESS(rv, rv);
   }
   return CreateMessage(originalMsgURI.get(), type, composeFields);
 }
 
 nsresult nsMsgCompose::SetDocumentCharset(const char *aCharset)
 {
+  NS_ENSURE_TRUE(m_compFields && m_editor, NS_ERROR_NOT_INITIALIZED);
+
   // Set charset, this will be used for the MIME charset labeling.
   m_compFields->SetCharacterSet(aCharset);
 
   // notify the change to editor
   nsCString charset;
   if (aCharset)
     charset = nsDependentCString(aCharset);
   if (m_editor)
@@ -3160,18 +3162,19 @@ NS_IMETHODIMP nsMsgCompose::GetType(MSG_
 
   *aType = mType;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsMsgCompose::QuoteMessage(const char *msgURI)
 {
-  nsresult    rv;
-
+  NS_ENSURE_ARG_POINTER(msgURI);
+
+  nsresult rv;
   mQuotingToFollow = false;
 
   // Create a mime parser (nsIStreamConverter)!
   mQuote = do_CreateInstance(NS_MSGQUOTE_CONTRACTID, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr <nsIMsgDBHdr> msgHdr;
   rv = GetMsgDBHdrFromURI(msgURI, getter_AddRefs(msgHdr));
--- a/mailnews/mime/emitters/src/nsMimeBaseEmitter.cpp
+++ b/mailnews/mime/emitters/src/nsMimeBaseEmitter.cpp
@@ -338,21 +338,19 @@ nsMimeBaseEmitter::SetOutputListener(nsI
 {
   mOutListener = listener;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsMimeBaseEmitter::GetOutputListener(nsIStreamListener **listener)
 {
-  if (listener)
-  {
-    *listener = mOutListener;
-    NS_IF_ADDREF(*listener);
-  }
+  NS_ENSURE_ARG_POINTER(listener);
+
+  NS_IF_ADDREF(*listener = mOutListener);
   return NS_OK;
 }
 
 
 // Attachment handling routines
 nsresult
 nsMimeBaseEmitter::StartAttachment(const nsACString &name,
                                    const char *contentType,
@@ -397,32 +395,36 @@ NS_IMETHODIMP
 nsMimeBaseEmitter::AddAttachmentField(const char *field, const char *value)
 {
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsMimeBaseEmitter::UtilityWrite(const char *buf)
 {
+  NS_ENSURE_ARG_POINTER(buf);
+
   PRUint32    written;
   Write(nsDependentCString(buf), &written);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsMimeBaseEmitter::UtilityWrite(const nsACString &buf)
 {
   PRUint32    written;
   Write(buf, &written);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsMimeBaseEmitter::UtilityWriteCRLF(const char *buf)
 {
+  NS_ENSURE_ARG_POINTER(buf);
+
   PRUint32    written;
   Write(nsDependentCString(buf), &written);
   Write(NS_LITERAL_CSTRING(CRLF), &written);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsMimeBaseEmitter::Write(const nsACString &buf, PRUint32 *amountWritten)
@@ -476,19 +478,19 @@ nsMimeBaseEmitter::Write(const nsACStrin
   }
 
   return rv;
 }
 
 nsresult
 nsMimeBaseEmitter::WriteHelper(const char *buf, PRUint32 count, PRUint32 *countWritten)
 {
-  nsresult rv;
+  NS_ENSURE_TRUE(mOutStream, NS_ERROR_NOT_INITIALIZED);
 
-  rv = mOutStream->Write(buf, count, countWritten);
+  nsresult rv = mOutStream->Write(buf, count, countWritten);
   if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
     // pipe is full, push contents of pipe to listener...
     PRUint32 avail;
     rv = mInputStream->Available(&avail);
     if (NS_SUCCEEDED(rv) && avail) {
       mOutListener->OnDataAvailable(mChannel, mURL, mInputStream, 0, avail);
 
       // try writing again...
@@ -534,29 +536,30 @@ nsMimeBaseEmitter::GetHeaderValue(const 
 // NOTE: This will be called even when headers are will not follow. This is
 // to allow us to be notified of the charset of the original message. This is
 // important for forward and reply operations
 //
 NS_IMETHODIMP
 nsMimeBaseEmitter::StartHeader(bool rootMailHeader, bool headerOnly, const char *msgID,
                                const char *outCharset)
 {
+  NS_ENSURE_ARG_POINTER(outCharset);
+
   mDocHeader = rootMailHeader;
 
   // If this is not the mail messages header, then we need to create
   // the mEmbeddedHeaderArray structure for use with this internal header
   // structure.
   if (!mDocHeader)
   {
     if (mEmbeddedHeaderArray)
       CleanupHeaderArray(mEmbeddedHeaderArray);
 
     mEmbeddedHeaderArray = new nsVoidArray();
-    if (!mEmbeddedHeaderArray)
-      return NS_ERROR_OUT_OF_MEMORY;
+    NS_ENSURE_TRUE(mEmbeddedHeaderArray, NS_ERROR_OUT_OF_MEMORY);
   }
 
   // If the main doc, check on updated character set
   if (mDocHeader)
     UpdateCharacterSet(outCharset);
   CopyASCIItoUTF16(nsDependentCString(outCharset), mCharset);
   return NS_OK;
 }
@@ -642,17 +645,17 @@ nsMimeBaseEmitter::AddAllHeaders(const n
 {
   if (mDocHeader) //We want to set only the main headers of a message, not the potentially embedded one
   {
     nsresult rv;
     nsCOMPtr<nsIMsgMailNewsUrl> msgurl (do_QueryInterface(mURL));
     if (msgurl)
     {
         nsCOMPtr<nsIMimeHeaders> mimeHeaders = do_CreateInstance(NS_IMIMEHEADERS_CONTRACTID, &rv);
-        NS_ENSURE_SUCCESS(rv,rv);
+        NS_ENSURE_SUCCESS(rv, rv);
         mimeHeaders->Initialize(allheaders.BeginReading(), allheaders.Length());
         msgurl->SetMimeHeaders(mimeHeaders);
     }
   }
   return NS_OK;
 }
 
 ////////////////////////////////////////////////////////////////////////////////