Bug 912465 - Use MsgNewSafeBufferedFileOutputStream instead of NewLocalFileOutputStream in various places. r=ehsan
authorJohannes Buchner <buchner.johannes@gmx.at>
Tue, 19 Nov 2013 09:13:44 -0500
changeset 156287 b8e6160eb82e
parent 156286 730f103c9f8f
child 156288 ad311f0dd591
push id25673
push userryanvm@gmail.com
push dateTue, 19 Nov 2013 18:22:02 +0000
treeherdermozilla-central@d316d1dd062e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs912465
milestone28.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 912465 - Use MsgNewSafeBufferedFileOutputStream instead of NewLocalFileOutputStream in various places. r=ehsan
extensions/spellcheck/src/mozPersonalDictionary.cpp
modules/libpref/src/Preferences.cpp
netwerk/base/src/nsBufferedStreams.cpp
--- a/extensions/spellcheck/src/mozPersonalDictionary.cpp
+++ b/extensions/spellcheck/src/mozPersonalDictionary.cpp
@@ -148,17 +148,17 @@ NS_IMETHODIMP mozPersonalDictionary::Sav
   //FIXME Deinst  -- get dictionary name from prefs;
   res = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(theFile));
   if(NS_FAILED(res)) return res;
   if(!theFile)return NS_ERROR_FAILURE;
   res = theFile->Append(NS_LITERAL_STRING(MOZ_PERSONAL_DICT_NAME));
   if(NS_FAILED(res)) return res;
 
   nsCOMPtr<nsIOutputStream> outStream;
-  NS_NewLocalFileOutputStream(getter_AddRefs(outStream), theFile, PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE ,0664);
+  NS_NewSafeLocalFileOutputStream(getter_AddRefs(outStream), theFile, PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE ,0664);
 
   // get a buffered output stream 4096 bytes big, to optimize writes
   nsCOMPtr<nsIOutputStream> bufferedOutputStream;
   res = NS_NewBufferedOutputStream(getter_AddRefs(bufferedOutputStream), outStream, 4096);
   if (NS_FAILED(res)) return res;
 
   nsTArray<nsString> array(mDictionaryTable.Count());
   mDictionaryTable.EnumerateEntries(AddHostToStringArray, &array);
@@ -166,16 +166,24 @@ NS_IMETHODIMP mozPersonalDictionary::Sav
   uint32_t bytesWritten;
   nsAutoCString utf8Key;
   for (uint32_t i = 0; i < array.Length(); ++i ) {
     CopyUTF16toUTF8(array[i], utf8Key);
 
     bufferedOutputStream->Write(utf8Key.get(), utf8Key.Length(), &bytesWritten);
     bufferedOutputStream->Write("\n", 1, &bytesWritten);
   }
+  nsCOMPtr<nsISafeOutputStream> safeStream = do_QueryInterface(bufferedOutputStream);
+  NS_ASSERTION(safeStream, "expected a safe output stream!");
+  if (safeStream) {
+    res = safeStream->Finish();
+    if (NS_FAILED(res)) {
+      NS_WARNING("failed to save personal dictionary file! possible data loss");
+    }
+  }
   return res;
 }
 
 /* readonly attribute nsIStringEnumerator GetWordList() */
 NS_IMETHODIMP mozPersonalDictionary::GetWordList(nsIStringEnumerator **aWords)
 {
   NS_ENSURE_ARG_POINTER(aWords);
   *aWords = nullptr;
--- a/modules/libpref/src/Preferences.cpp
+++ b/modules/libpref/src/Preferences.cpp
@@ -934,17 +934,17 @@ Preferences::WritePrefFile(nsIFile* aFil
 
   // tell the safe output stream to overwrite the real prefs file
   // (it'll abort if there were any errors during writing)
   nsCOMPtr<nsISafeOutputStream> safeStream = do_QueryInterface(outStream);
   NS_ASSERTION(safeStream, "expected a safe output stream!");
   if (safeStream) {
     rv = safeStream->Finish();
     if (NS_FAILED(rv)) {
-      NS_WARNING("failed to save prefs file! possible dataloss");
+      NS_WARNING("failed to save prefs file! possible data loss");
       return rv;
     }
   }
 
   gDirty = false;
   return NS_OK;
 }
 
--- a/netwerk/base/src/nsBufferedStreams.cpp
+++ b/netwerk/base/src/nsBufferedStreams.cpp
@@ -624,17 +624,17 @@ nsBufferedOutputStream::Write(const char
 }
 
 NS_IMETHODIMP
 nsBufferedOutputStream::Flush()
 {
     nsresult rv;
     uint32_t amt;
     if (!mStream) {
-        // Stream already cancelled/flushed; probably because of error.
+        // Stream already cancelled/flushed; probably because of previous error.
         return NS_OK;
     }
     rv = Sink()->Write(mBuffer, mFillPoint, &amt);
     if (NS_FAILED(rv)) return rv;
     mBufferStartOffset += amt;
     if (amt == mFillPoint) {
         mFillPoint = mCursor = 0;
         return NS_OK;   // flushed everything