Bug 1287292 - Use fallible unicode conversion in XHR. r=baku
authorEric Rahm <erahm@mozilla.com>
Thu, 08 Sep 2016 16:50:38 -0700
changeset 354648 8a994ee68b54d73bb9b5a574ec32f8e87c323689
parent 354647 872fcfa1267903bd894442e2385ac3f5b2e3f702
child 354649 7463c762e71934161e585d3a41237c604f688adb
push id6570
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:26:13 +0000
treeherdermozilla-beta@f455459b2ae5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1287292
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 1287292 - Use fallible unicode conversion in XHR. r=baku When sending a string via XHR use fallible conversion to avoid OOMing if content provides a particularly large string.
dom/xhr/XMLHttpRequestMainThread.cpp
--- a/dom/xhr/XMLHttpRequestMainThread.cpp
+++ b/dom/xhr/XMLHttpRequestMainThread.cpp
@@ -2227,17 +2227,21 @@ XMLHttpRequestMainThread::RequestBody<ns
 
   if (mBody->IsHTMLDocument()) {
     aContentType.AssignLiteral("text/html");
 
     nsString serialized;
     if (!nsContentUtils::SerializeNodeToMarkup(mBody, true, serialized)) {
       return NS_ERROR_OUT_OF_MEMORY;
     }
-    NS_ConvertUTF16toUTF8 utf8Serialized(serialized);
+
+    nsAutoCString utf8Serialized;
+    if (!AppendUTF16toUTF8(serialized, utf8Serialized, fallible)) {
+      return NS_ERROR_OUT_OF_MEMORY;
+    }
 
     uint32_t written;
     rv = output->Write(utf8Serialized.get(), utf8Serialized.Length(), &written);
     NS_ENSURE_SUCCESS(rv, rv);
 
     MOZ_ASSERT(written == utf8Serialized.Length());
   } else {
     aContentType.AssignLiteral("application/xml");
@@ -2266,17 +2270,21 @@ XMLHttpRequestMainThread::RequestBody<ns
 template<> nsresult
 XMLHttpRequestMainThread::RequestBody<const nsAString>::GetAsStream(
    nsIInputStream** aResult, uint64_t* aContentLength,
    nsACString& aContentType, nsACString& aCharset) const
 {
   aContentType.AssignLiteral("text/plain");
   aCharset.AssignLiteral("UTF-8");
 
-  nsCString converted = NS_ConvertUTF16toUTF8(*mBody);
+  nsAutoCString converted;
+  if (!AppendUTF16toUTF8(*mBody, converted, fallible)) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
+
   *aContentLength = converted.Length();
   nsresult rv = NS_NewCStringInputStream(aResult, converted);
   NS_ENSURE_SUCCESS(rv, rv);
   return NS_OK;
 }
 
 template<> nsresult
 XMLHttpRequestMainThread::RequestBody<nsIInputStream>::GetAsStream(