Bug 877164 - Crash if document init fails. r=smaug, a=lsblakk
authorGabor Krizsanits <gkrizsanits@mozilla.com>
Mon, 29 Jul 2013 13:07:30 +0200
changeset 143154 ceff00cca022f4eb586b8eda8cc941727b88d001
parent 143153 25ac5bb0b22b32f92e0b46d9f8dce2ca01c986a8
child 143155 70568b0ba5b6214ee6c2801439e3d37ec0070626
push id2684
push usergkrizsanits@mozilla.com
push dateMon, 29 Jul 2013 11:08:23 +0000
treeherdermozilla-beta@ceff00cca022 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, lsblakk
bugs877164
milestone23.0
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
@@ -173,30 +173,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)
 {