Bug 1305422 - part 2 - don't call size_forward on ns*String::iterators to check for SetLength failure; r=erahm
authorNathan Froyd <froydnj@mozilla.com>
Thu, 29 Sep 2016 22:33:58 -0400
changeset 315872 8313868b4334a07005ec359e6b0745ee395f10ad
parent 315871 77a8d7d6a0fb5fb4cdce1c9edf68a573f7d3de55
child 315873 e208dfe24c94f7c3cddbc27df9e49d7fadc2acaf
push id20634
push usercbook@mozilla.com
push dateFri, 30 Sep 2016 10:10:13 +0000
treeherderfx-team@afe79b010d13 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerserahm
bugs1305422
milestone52.0a1
Bug 1305422 - part 2 - don't call size_forward on ns*String::iterators to check for SetLength failure; r=erahm We have better ways of checking for SetLength failure nowadays, and even if these SetLength calls did fail, the program would crash anyway.
toolkit/xre/nsXREDirProvider.cpp
xpcom/ds/nsWindowsRegKey.cpp
--- a/toolkit/xre/nsXREDirProvider.cpp
+++ b/toolkit/xre/nsXREDirProvider.cpp
@@ -1310,25 +1310,25 @@ GetRegWindowsAppDataFolder(bool aLocal, 
     ::RegCloseKey(key);
     _retval.SetLength(0);
     return NS_ERROR_NOT_AVAILABLE;
   }
 
   // |size| may or may not include room for the terminating null character
   DWORD resultLen = size / 2;
 
-  _retval.SetLength(resultLen);
-  nsAString::iterator begin;
-  _retval.BeginWriting(begin);
-  if (begin.size_forward() != resultLen) {
+  if (!_retval.SetLength(resultLen, mozilla::fallible)) {
     ::RegCloseKey(key);
     _retval.SetLength(0);
     return NS_ERROR_NOT_AVAILABLE;
   }
 
+  nsAString::iterator begin;
+  _retval.BeginWriting(begin);
+
   res = RegQueryValueExW(key, (aLocal ? L"Local AppData" : L"AppData"),
                          nullptr, nullptr, (LPBYTE) begin.get(), &size);
   ::RegCloseKey(key);
   if (res != ERROR_SUCCESS) {
     _retval.SetLength(0);
     return NS_ERROR_NOT_AVAILABLE;
   }
 
--- a/xpcom/ds/nsWindowsRegKey.cpp
+++ b/xpcom/ds/nsWindowsRegKey.cpp
@@ -322,22 +322,22 @@ nsWindowsRegKey::ReadStringValue(const n
   if (size == 0) {
     aResult.Truncate();
     return NS_OK;
   }
 
   // |size| may or may not include the terminating null character.
   DWORD resultLen = size / 2;
 
-  aResult.SetLength(resultLen);
+  if (!aResult.SetLength(resultLen, mozilla::fallible)) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
+
   nsAString::iterator begin;
   aResult.BeginWriting(begin);
-  if (begin.size_forward() != resultLen) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
 
   rv = RegQueryValueExW(mKey, flatName.get(), 0, &type, (LPBYTE)begin.get(),
                         &size);
 
   if (!aResult.CharAt(resultLen - 1)) {
     // The string passed to us had a null terminator in the final position.
     aResult.Truncate(resultLen - 1);
   }
@@ -345,22 +345,22 @@ nsWindowsRegKey::ReadStringValue(const n
   // Expand the environment variables if needed
   if (type == REG_EXPAND_SZ) {
     const nsString& flatSource = PromiseFlatString(aResult);
     resultLen = ExpandEnvironmentStringsW(flatSource.get(), nullptr, 0);
     if (resultLen > 1) {
       nsAutoString expandedResult;
       // |resultLen| includes the terminating null character
       --resultLen;
-      expandedResult.SetLength(resultLen);
+      if (expandedResult.SetLength(resultLen, mozilla::fallible)) {
+        return NS_ERROR_OUT_OF_MEMORY;
+      }
+
       nsAString::iterator begin;
       expandedResult.BeginWriting(begin);
-      if (begin.size_forward() != resultLen) {
-        return NS_ERROR_OUT_OF_MEMORY;
-      }
 
       resultLen = ExpandEnvironmentStringsW(flatSource.get(),
                                             wwc(begin.get()),
                                             resultLen + 1);
       if (resultLen <= 0) {
         rv = ERROR_UNKNOWN_FEATURE;
         aResult.Truncate();
       } else {
@@ -417,22 +417,22 @@ nsWindowsRegKey::ReadBinaryValue(const n
     return NS_ERROR_FAILURE;
   }
 
   if (!size) {
     aResult.Truncate();
     return NS_OK;
   }
 
-  aResult.SetLength(size);
+  if (aResult.SetLength(size, mozilla::fallible)) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
+
   nsACString::iterator begin;
   aResult.BeginWriting(begin);
-  if (begin.size_forward() != size) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
 
   rv = RegQueryValueExW(mKey, PromiseFlatString(aName).get(), 0, nullptr,
                         (LPBYTE)begin.get(), &size);
   return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP
 nsWindowsRegKey::WriteStringValue(const nsAString& aName,