Bug 877164, return early if nsDocument::Init fails, r=smaug
authorgkrizsanits@mozilla.com
Sat, 27 Jul 2013 12:12:38 +0300
changeset 140220 a0c2043c95e392dd509c522e40c92b59ba56a4af
parent 140209 fb48c7d58b8be9ce281d9dc032aaa10d5125efd0
child 140221 6d730e476cf4abda9752b872a054bef8b1a19f92
push id1951
push userryanvm@gmail.com
push dateSun, 28 Jul 2013 01:55:51 +0000
treeherderfx-team@73b69c146ca6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs877164
milestone25.0a1
Bug 877164, return early if nsDocument::Init fails, r=smaug
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)
 {