Bug 1303095 - XMLHttpRequestString should be fallible, r=mccr8
authorAndrea Marchesini <amarchesini@mozilla.com>
Fri, 16 Sep 2016 00:09:17 +0200
changeset 314179 5288360655dda563e23ea8196f5029b94007d222
parent 314178 c3f82cedfb27bb9a0b78fa10d99ec384538745c0
child 314180 ab1716e1d3741cf09ac579194ddf4b5125df0ca9
push id30710
push usercbook@mozilla.com
push dateFri, 16 Sep 2016 10:56:55 +0000
treeherdermozilla-central@f1dbeb5dee22 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmccr8
bugs1303095
milestone51.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 1303095 - XMLHttpRequestString should be fallible, r=mccr8
dom/xhr/XMLHttpRequestMainThread.cpp
dom/xhr/XMLHttpRequestString.cpp
dom/xhr/XMLHttpRequestString.h
dom/xhr/XMLHttpRequestWorker.cpp
--- a/dom/xhr/XMLHttpRequestMainThread.cpp
+++ b/dom/xhr/XMLHttpRequestMainThread.cpp
@@ -576,17 +576,20 @@ XMLHttpRequestMainThread::GetResponseTex
                                           ErrorResult& aRv)
 {
   XMLHttpRequestStringSnapshot snapshot;
   GetResponseText(snapshot, aRv);
   if (aRv.Failed()) {
     return;
   }
 
-  snapshot.GetAsString(aResponseText);
+  if (!snapshot.GetAsString(aResponseText)) {
+    aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
+    return;
+  }
 }
 
 void
 XMLHttpRequestMainThread::GetResponseText(XMLHttpRequestStringSnapshot& aSnapshot,
                                           ErrorResult& aRv)
 {
   aSnapshot.Reset();
 
@@ -640,17 +643,19 @@ XMLHttpRequestMainThread::GetResponseTex
 nsresult
 XMLHttpRequestMainThread::CreateResponseParsedJSON(JSContext* aCx)
 {
   if (!aCx) {
     return NS_ERROR_FAILURE;
   }
 
   nsAutoString string;
-  mResponseText.GetAsString(string);
+  if (!mResponseText.GetAsString(string)) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
 
   // The Unicode converter has already zapped the BOM if there was one
   JS::Rooted<JS::Value> value(aCx);
   if (!JS_ParseJSON(aCx, string.BeginReading(), string.Length(), &value)) {
     return NS_ERROR_FAILURE;
   }
 
   mResultJSON = value;
--- a/dom/xhr/XMLHttpRequestString.cpp
+++ b/dom/xhr/XMLHttpRequestString.cpp
@@ -42,35 +42,35 @@ public:
   Append(const nsAString& aString)
   {
     NS_ASSERT_OWNINGTHREAD(XMLHttpRequestStringBuffer);
 
     MutexAutoLock lock(mMutex);
     mData.Append(aString);
   }
 
-  void
+  MOZ_MUST_USE bool
   GetAsString(nsAString& aString)
   {
     MutexAutoLock lock(mMutex);
-    aString = mData;
+    return aString.Assign(mData, mozilla::fallible);
   }
 
   size_t
   SizeOfThis(MallocSizeOf aMallocSizeOf) const
   {
     return mData.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
   }
 
-  void
+  MOZ_MUST_USE bool
   GetAsString(nsAString& aString, uint32_t aLength)
   {
     MutexAutoLock lock(mMutex);
     MOZ_ASSERT(aLength <= mData.Length());
-    aString.Assign(mData.BeginReading(), aLength);
+    return aString.Assign(mData.BeginReading(), aLength, mozilla::fallible);
   }
 
   void
   CreateSnapshot(XMLHttpRequestStringSnapshot& aSnapshot)
   {
     MutexAutoLock lock(mMutex);
     aSnapshot.Set(this, mData.Length());
   }
@@ -115,17 +115,17 @@ XMLHttpRequestString::Length() const
 }
 
 void
 XMLHttpRequestString::Append(const nsAString& aString)
 {
   mBuffer->Append(aString);
 }
 
-void
+bool
 XMLHttpRequestString::GetAsString(nsAString& aString) const
 {
   return mBuffer->GetAsString(aString);
 }
 
 size_t
 XMLHttpRequestString::SizeOfThis(MallocSizeOf aMallocSizeOf) const
 {
@@ -181,30 +181,31 @@ XMLHttpRequestStringSnapshot::Set(XMLHtt
   MOZ_ASSERT(aBuffer);
   MOZ_ASSERT(aLength <= aBuffer->UnsafeLength());
 
   mBuffer = aBuffer;
   mLength = aLength;
   mVoid = false;
 }
 
-void
+bool
 XMLHttpRequestStringSnapshot::GetAsString(nsAString& aString) const
 {
   if (mBuffer) {
     MOZ_ASSERT(!mVoid);
-    mBuffer->GetAsString(aString, mLength);
-    return;
+    return mBuffer->GetAsString(aString, mLength);
   }
 
   aString.Truncate();
 
   if (mVoid) {
     aString.SetIsVoid(true);
   }
+
+  return true;
 }
 
 // ---------------------------------------------------------------------------
 // XMLHttpRequestStringWriterHelper
 
 XMLHttpRequestStringWriterHelper::XMLHttpRequestStringWriterHelper(XMLHttpRequestString& aString)
   : mBuffer(aString.mBuffer)
   , mLock(aString.mBuffer->mMutex)
--- a/dom/xhr/XMLHttpRequestString.h
+++ b/dom/xhr/XMLHttpRequestString.h
@@ -36,17 +36,17 @@ public:
 
   uint32_t Length() const;
 
   void Append(const nsAString& aString);
 
   // This method should be called only when the string is really needed because
   // it can cause the duplication of the strings in case the loading of the XHR
   // is not completed yet.
-  void GetAsString(nsAString& aString) const;
+  MOZ_MUST_USE bool GetAsString(nsAString& aString) const;
 
   size_t SizeOfThis(MallocSizeOf aMallocSizeOf) const;
 
   const char16_t* Data() const;
 
   bool IsEmpty() const;
 
   void CreateSnapshot(XMLHttpRequestStringSnapshot& aSnapshot);
@@ -113,17 +113,17 @@ public:
     return mVoid;
   }
 
   bool IsEmpty() const
   {
     return !mLength;
   }
 
-  void GetAsString(nsAString& aString) const;
+  MOZ_MUST_USE bool GetAsString(nsAString& aString) const;
 
 private:
   XMLHttpRequestStringSnapshot(const XMLHttpRequestStringSnapshot&) = delete;
   XMLHttpRequestStringSnapshot& operator=(const XMLHttpRequestStringSnapshot&&) = delete;
 
   void Set(XMLHttpRequestStringBuffer* aBuffer, uint32_t aLength);
 
   void ResetInternal(bool aIsVoid);
--- a/dom/xhr/XMLHttpRequestWorker.cpp
+++ b/dom/xhr/XMLHttpRequestWorker.cpp
@@ -2420,19 +2420,20 @@ XMLHttpRequestWorker::GetResponse(JSCont
 void
 XMLHttpRequestWorker::GetResponseText(nsAString& aResponseText, ErrorResult& aRv)
 {
   aRv = mStateData.mResponseTextResult;
   if (aRv.Failed()) {
     return;
   }
 
-  nsAutoString foo;
-  mStateData.mResponseText.GetAsString(foo);
-  aResponseText.Assign(foo.BeginReading(), foo.Length());
+  if (!mStateData.mResponseText.GetAsString(aResponseText)) {
+    aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
+    return;
+  }
 }
 
 void
 XMLHttpRequestWorker::UpdateState(const StateData& aStateData,
                                   bool aUseCachedArrayBufferResponse)
 {
   if (aUseCachedArrayBufferResponse) {
     MOZ_ASSERT(mStateData.mResponse.isObject() &&