From 8ac96143cf4b80f74215cbfbfaa283f006044510 Mon Sep 17 00:00:00 2001 From: "jst%mozilla.jstenback.com" Date: Mon, 23 Feb 2004 23:06:25 +0000 Subject: [PATCH] Fixing string leak bug 235088. r=darin@meer.net, sr=mscott@mozilla.org --- mailnews/base/src/nsMessenger.cpp | 70 ++++++++++++++++--------------- mailnews/base/src/nsMessenger.h | 13 +++--- 2 files changed, 43 insertions(+), 40 deletions(-) diff --git a/mailnews/base/src/nsMessenger.cpp b/mailnews/base/src/nsMessenger.cpp index a80457e6bb5..95e74237cc9 100644 --- a/mailnews/base/src/nsMessenger.cpp +++ b/mailnews/base/src/nsMessenger.cpp @@ -516,7 +516,10 @@ nsMessenger::PromptIfFileExists(nsFileSpec &fileSpec) nsCOMPtr filePicker = do_CreateInstance("@mozilla.org/filepicker;1", &rv); if (NS_FAILED(rv)) return rv; - filePicker->Init(nsnull, GetString(NS_LITERAL_STRING("SaveAttachment").get()), nsIFilePicker::modeSave); + + filePicker->Init(nsnull, + GetString(NS_LITERAL_STRING("SaveAttachment")).get(), + nsIFilePicker::modeSave); filePicker->SetDefaultString(path.get()); filePicker->AppendFilters(nsIFilePicker::filterAll); @@ -537,7 +540,7 @@ nsMessenger::PromptIfFileExists(nsFileSpec &fileSpec) nsCOMPtr localFile; nsCAutoString filePath; - + rv = filePicker->GetFile(getter_AddRefs(localFile)); if (NS_FAILED(rv)) return rv; @@ -806,11 +809,8 @@ nsMessenger::SaveAttachment(const char * contentType, const char * url, rv = ConvertAndSanitizeFileName(displayName, getter_Copies(defaultDisplayString), nsnull); if (NS_FAILED(rv)) goto done; - filePicker->Init( - nsnull, - GetString(NS_LITERAL_STRING("SaveAttachment").get()), - nsIFilePicker::modeSave - ); + filePicker->Init(nsnull, GetString(NS_LITERAL_STRING("SaveAttachment")).get(), + nsIFilePicker::modeSave); filePicker->SetDefaultString(defaultDisplayString.get()); filePicker->AppendFilters(nsIFilePicker::filterAll); @@ -858,11 +858,10 @@ nsMessenger::SaveAllAttachments(PRUint32 count, PRInt16 dialogResult; if (NS_FAILED(rv)) goto done; - filePicker->Init( - nsnull, - GetString(NS_LITERAL_STRING("SaveAllAttachments").get()), - nsIFilePicker::modeGetFolder - ); + + filePicker->Init(nsnull, + GetString(NS_LITERAL_STRING("SaveAllAttachments")).get(), + nsIFilePicker::modeGetFolder); rv = GetLastSaveDirectory(getter_AddRefs(lastSaveDir)); if (NS_SUCCEEDED(rv) && lastSaveDir) { @@ -949,20 +948,22 @@ nsMessenger::SaveAs(const char *aURI, PRBool aAsFile, nsIMsgIdentity *aIdentity, nsCOMPtr filePicker = do_CreateInstance("@mozilla.org/filepicker;1", &rv); if (NS_FAILED(rv)) goto done; - - filePicker->Init(nsnull, GetString(NS_LITERAL_STRING("SaveMailAs").get()), nsIFilePicker::modeSave); + + filePicker->Init(nsnull, GetString(NS_LITERAL_STRING("SaveMailAs")).get(), + nsIFilePicker::modeSave); // if we have a non-null filename use it, otherwise use default save message one if (aMsgFilename) filePicker->SetDefaultString(aMsgFilename); - else - filePicker->SetDefaultString(GetString(NS_LITERAL_STRING("defaultSaveMessageAsFileName").get())); - + else { + filePicker->SetDefaultString(GetString(NS_LITERAL_STRING("defaultSaveMessageAsFileName")).get()); + } + // because we will be using GetFilterIndex() // we must call AppendFilters() one at a time, // in MESSENGER_SAVEAS_FILE_TYPE order - filePicker->AppendFilter(GetString(NS_LITERAL_STRING("EMLFiles").get()), - NS_LITERAL_STRING("*.eml").get()); + filePicker->AppendFilter(GetString(NS_LITERAL_STRING("EMLFiles")).get(), + NS_LITERAL_STRING("*.eml").get()); filePicker->AppendFilters(nsIFilePicker::filterHTML); filePicker->AppendFilters(nsIFilePicker::filterText); filePicker->AppendFilters(nsIFilePicker::filterAll); @@ -1209,13 +1210,14 @@ nsresult nsMessenger::Alert(const char *stringName) { nsresult rv = NS_OK; - nsString errorMessage(GetString(NS_ConvertASCIItoUCS2(stringName).get())); + if (mDocShell) { nsCOMPtr dialog(do_GetInterface(mDocShell)); - + if (dialog) { - rv = dialog->Alert(nsnull, errorMessage.get()); + rv = dialog->Alert(nsnull, + GetString(NS_ConvertASCIItoUCS2(stringName)).get()); } } return rv; @@ -2071,22 +2073,22 @@ nsMessenger::InitStringBundle() return res; } -PRUnichar * -nsMessenger::GetString(const PRUnichar *aStringName) +nsAdoptingString +nsMessenger::GetString(const nsAFlatString& aStringName) { - nsresult res = NS_OK; - PRUnichar *ptrv = nsnull; + nsresult rv = NS_OK; + PRUnichar *ptrv = nsnull; - if (!mStringBundle) - res = InitStringBundle(); + if (!mStringBundle) + rv = InitStringBundle(); - if (mStringBundle) - res = mStringBundle->GetStringFromName(aStringName, &ptrv); + if (mStringBundle) + rv = mStringBundle->GetStringFromName(aStringName.get(), &ptrv); - if ( NS_SUCCEEDED(res) && (ptrv) ) - return ptrv; - else - return nsCRT::strdup(aStringName); + if (NS_FAILED(rv) || !ptrv) + ptrv = ToNewUnicode(aStringName); + + return nsAdoptingString(ptrv); } nsSaveAllAttachmentsState::nsSaveAllAttachmentsState(PRUint32 count, diff --git a/mailnews/base/src/nsMessenger.h b/mailnews/base/src/nsMessenger.h index b89921f5725..c4432acd14b 100644 --- a/mailnews/base/src/nsMessenger.h +++ b/mailnews/base/src/nsMessenger.h @@ -58,7 +58,7 @@ public: NS_DECL_ISUPPORTS NS_DECL_NSIMESSENGER - NS_DECL_NSIOBSERVER + NS_DECL_NSIOBSERVER nsresult Alert(const char * stringName); nsresult SaveAttachment(nsIFileSpec *fileSpec, const char* unescapedUrl, @@ -67,10 +67,11 @@ public: nsresult PromptIfFileExists(nsFileSpec &fileSpec); protected: - nsresult DoDelete(nsIRDFCompositeDataSource* db, nsISupportsArray *srcArray, nsISupportsArray *deletedArray); - nsresult DoCommand(nsIRDFCompositeDataSource *db, const nsACString& command, nsISupportsArray *srcArray, - nsISupportsArray *arguments); - PRUnichar *GetString(const PRUnichar *aStringName); + nsresult DoDelete(nsIRDFCompositeDataSource* db, nsISupportsArray *srcArray, + nsISupportsArray *deletedArray); + nsresult DoCommand(nsIRDFCompositeDataSource *db, const nsACString& command, + nsISupportsArray *srcArray, nsISupportsArray *arguments); + nsAdoptingString GetString(const nsAFlatString& aStringName); nsresult InitStringBundle(); private: @@ -102,7 +103,7 @@ private: #define NS_MESSENGER_CID \ { /* 3f181950-c14d-11d2-b7f2-00805f05ffa5 */ \ - 0x3f181950, 0xc14d, 0x11d2, \ + 0x3f181950, 0xc14d, 0x11d2, \ {0xb7, 0xf2, 0x0, 0x80, 0x5f, 0x05, 0xff, 0xa5}}