Bug 877164 - Crash if document init fails. r=smaug, a=lsblakk
authorGabor Krizsanits <gkrizsanits@mozilla.com>
Mon, 29 Jul 2013 12:19:03 +0200
changeset 148115 e5037c3603855ce3f2e57c801a1f9a3d6bcc8e66
parent 148114 541b80da91fa7a42bc68f13e931e2e849fad74a5
child 148116 f93d5a353d6631e8562fa074f2749591a0f0295a
push id2697
push userbbajaj@mozilla.com
push dateMon, 05 Aug 2013 18:49:53 +0000
treeherdermozilla-beta@dfec938c7b63 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, lsblakk
bugs877164
milestone24.0a2
Bug 877164 - Crash if document init fails. r=smaug, a=lsblakk
content/html/document/src/nsHTMLDocument.cpp
content/xml/document/src/XMLDocument.cpp
--- a/content/html/document/src/nsHTMLDocument.cpp
+++ b/content/html/document/src/nsHTMLDocument.cpp
@@ -164,30 +164,29 @@ RemoveFromAgentSheets(nsCOMArray<nsIStyl
   }
 
   return NS_OK;
 }
 
 nsresult
 NS_NewHTMLDocument(nsIDocument** aInstancePtrResult, bool aLoadedAsData)
 {
-  nsHTMLDocument* doc = new nsHTMLDocument();
-  NS_ENSURE_TRUE(doc, NS_ERROR_OUT_OF_MEMORY);
-
-  NS_ADDREF(doc);
+  nsRefPtr<nsHTMLDocument> doc = new nsHTMLDocument();
+
   nsresult rv = doc->Init();
 
   if (NS_FAILED(rv)) {
-    NS_RELEASE(doc);
+    *aInstancePtrResult = nullptr;
+    return rv;
   }
 
-  *aInstancePtrResult = doc;
   doc->SetLoadedAsData(aLoadedAsData);
-
-  return rv;
+  doc.forget(aInstancePtrResult);
+
+  return NS_OK;
 }
 
   // NOTE! nsDocument::operator new() zeroes out all members, so don't
   // bother initializing members to 0.
 
 nsHTMLDocument::nsHTMLDocument()
   : nsDocument("text/html")
 {
--- a/content/xml/document/src/XMLDocument.cpp
+++ b/content/xml/document/src/XMLDocument.cpp
@@ -168,34 +168,32 @@ NS_NewDOMDocument(nsIDOMDocument** aInst
   }
 
   *aInstancePtrResult = doc;
   NS_ADDREF(*aInstancePtrResult);
 
   return NS_OK;
 }
 
-
 nsresult
 NS_NewXMLDocument(nsIDocument** aInstancePtrResult, bool aLoadedAsData)
 {
-  XMLDocument* doc = new XMLDocument();
-  NS_ENSURE_TRUE(doc, NS_ERROR_OUT_OF_MEMORY);
+  nsRefPtr<XMLDocument> doc = new XMLDocument();
 
-  NS_ADDREF(doc);
   nsresult rv = doc->Init();
 
   if (NS_FAILED(rv)) {
-    NS_RELEASE(doc);
+    *aInstancePtrResult = nullptr;
+    return rv;
   }
 
-  *aInstancePtrResult = doc;
   doc->SetLoadedAsData(aLoadedAsData);
+  doc.forget(aInstancePtrResult);
 
-  return rv;
+  return NS_OK;
 }
 
 nsresult
 NS_NewXBLDocument(nsIDOMDocument** aInstancePtrResult,
                   nsIURI* aDocumentURI,
                   nsIURI* aBaseURI,
                   nsIPrincipal* aPrincipal)
 {