Bug 780164 - Make nsAttrAndChildArray::GetModifiableMapped infallible; r=bz
authorMs2ger <ms2ger@gmail.com>
Sat, 04 Aug 2012 09:44:01 +0200
changeset 106820 20fc34efd73393a064b90360f2d8a72bcd1677b7
parent 106819 3dceba4ba1229a6040eed477d98af6f8f2033136
child 106821 d75ca2cb6a2cc1850a737ae90a8a13e0f88fc81e
child 106894 197ea74511d5030c36f60c2ebee8e3ce6becffd0
push id1490
push userakeybl@mozilla.com
push dateMon, 08 Oct 2012 18:29:50 +0000
treeherdermozilla-beta@f335e7dacdc1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs780164
milestone17.0a1
first release with
nightly linux32
20fc34efd733 / 17.0a1 / 20120804030551 / files
nightly linux64
20fc34efd733 / 17.0a1 / 20120804030551 / files
nightly mac
20fc34efd733 / 17.0a1 / 20120804030551 / files
nightly win32
20fc34efd733 / 17.0a1 / 20120804030551 / files
nightly win64
20fc34efd733 / 17.0a1 / 20120804030551 / files
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
releases
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 780164 - Make nsAttrAndChildArray::GetModifiableMapped infallible; r=bz
content/base/src/nsAttrAndChildArray.cpp
content/base/src/nsAttrAndChildArray.h
content/base/src/nsMappedAttributes.cpp
content/base/src/nsMappedAttributes.h
--- a/content/base/src/nsAttrAndChildArray.cpp
+++ b/content/base/src/nsAttrAndChildArray.cpp
@@ -439,20 +439,18 @@ nsAttrAndChildArray::RemoveAttrAt(PRUint
       // We're removing the last mapped attribute.  Can't swap in this
       // case; have to copy.
       aValue.SetTo(*mImpl->mMappedAttrs->AttrAt(0));
       NS_RELEASE(mImpl->mMappedAttrs);
 
       return NS_OK;
     }
 
-    nsRefPtr<nsMappedAttributes> mapped;
-    nsresult rv = GetModifiableMapped(nullptr, nullptr, false,
-                                      getter_AddRefs(mapped));
-    NS_ENSURE_SUCCESS(rv, rv);
+    nsRefPtr<nsMappedAttributes> mapped =
+      GetModifiableMapped(nullptr, nullptr, false);
 
     mapped->RemoveAttrAt(aPos, aValue);
 
     return MakeMappedUnique(mapped);
   }
 
   aPos -= mapped;
   ATTRS(mImpl)[aPos].mValue.SwapValueWith(aValue);
@@ -553,46 +551,41 @@ nsAttrAndChildArray::IndexOfAttr(nsIAtom
 }
 
 nsresult
 nsAttrAndChildArray::SetAndTakeMappedAttr(nsIAtom* aLocalName,
                                           nsAttrValue& aValue,
                                           nsMappedAttributeElement* aContent,
                                           nsHTMLStyleSheet* aSheet)
 {
-  nsRefPtr<nsMappedAttributes> mapped;
-
   bool willAdd = true;
   if (mImpl && mImpl->mMappedAttrs) {
-    willAdd = mImpl->mMappedAttrs->GetAttr(aLocalName) == nullptr;
+    willAdd = !mImpl->mMappedAttrs->GetAttr(aLocalName);
   }
 
-  nsresult rv = GetModifiableMapped(aContent, aSheet, willAdd,
-                                    getter_AddRefs(mapped));
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsRefPtr<nsMappedAttributes> mapped =
+    GetModifiableMapped(aContent, aSheet, willAdd);
 
-  rv = mapped->SetAndTakeAttr(aLocalName, aValue);
+  nsresult rv = mapped->SetAndTakeAttr(aLocalName, aValue);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return MakeMappedUnique(mapped);
 }
 
 nsresult
 nsAttrAndChildArray::DoSetMappedAttrStyleSheet(nsHTMLStyleSheet* aSheet)
 {
   NS_PRECONDITION(mImpl && mImpl->mMappedAttrs,
                   "Should have mapped attrs here!");
   if (aSheet == mImpl->mMappedAttrs->GetStyleSheet()) {
     return NS_OK;
   }
 
-  nsRefPtr<nsMappedAttributes> mapped;
-  nsresult rv = GetModifiableMapped(nullptr, nullptr, false, 
-                                    getter_AddRefs(mapped));
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsRefPtr<nsMappedAttributes> mapped =
+    GetModifiableMapped(nullptr, nullptr, false);
 
   mapped->SetStyleSheet(aSheet);
 
   return MakeMappedUnique(mapped);
 }
 
 void
 nsAttrAndChildArray::WalkMappedAttributeStyleRules(nsRuleWalker* aRuleWalker)
@@ -692,43 +685,30 @@ nsAttrAndChildArray::NonMappedAttrCount(
 }
 
 PRUint32
 nsAttrAndChildArray::MappedAttrCount() const
 {
   return mImpl && mImpl->mMappedAttrs ? (PRUint32)mImpl->mMappedAttrs->Count() : 0;
 }
 
-nsresult
+nsMappedAttributes*
 nsAttrAndChildArray::GetModifiableMapped(nsMappedAttributeElement* aContent,
                                          nsHTMLStyleSheet* aSheet,
-                                         bool aWillAddAttr,
-                                         nsMappedAttributes** aModifiable)
+                                         bool aWillAddAttr)
 {
-  *aModifiable = nullptr;
-
   if (mImpl && mImpl->mMappedAttrs) {
-    *aModifiable = mImpl->mMappedAttrs->Clone(aWillAddAttr);
-    NS_ENSURE_TRUE(*aModifiable, NS_ERROR_OUT_OF_MEMORY);
-
-    NS_ADDREF(*aModifiable);
-    
-    return NS_OK;
+    return mImpl->mMappedAttrs->Clone(aWillAddAttr);
   }
 
-  NS_ASSERTION(aContent, "Trying to create modifiable without content");
+  MOZ_ASSERT(aContent, "Trying to create modifiable without content");
 
   nsMapRuleToAttributesFunc mapRuleFunc =
     aContent->GetAttributeMappingFunction();
-  *aModifiable = new nsMappedAttributes(aSheet, mapRuleFunc);
-  NS_ENSURE_TRUE(*aModifiable, NS_ERROR_OUT_OF_MEMORY);
-
-  NS_ADDREF(*aModifiable);
-
-  return NS_OK;
+  return new nsMappedAttributes(aSheet, mapRuleFunc);
 }
 
 nsresult
 nsAttrAndChildArray::MakeMappedUnique(nsMappedAttributes* aAttributes)
 {
   NS_ASSERTION(aAttributes, "missing attributes");
 
   if (!mImpl && !GrowBy(1)) {
--- a/content/base/src/nsAttrAndChildArray.h
+++ b/content/base/src/nsAttrAndChildArray.h
@@ -122,20 +122,21 @@ private:
   nsAttrAndChildArray(const nsAttrAndChildArray& aOther) MOZ_DELETE;
   nsAttrAndChildArray& operator=(const nsAttrAndChildArray& aOther) MOZ_DELETE;
 
   void Clear();
 
   PRUint32 NonMappedAttrCount() const;
   PRUint32 MappedAttrCount() const;
 
-  nsresult GetModifiableMapped(nsMappedAttributeElement* aContent,
-                               nsHTMLStyleSheet* aSheet,
-                               bool aWillAddAttr,
-                               nsMappedAttributes** aModifiable);
+  // Returns a non-null zero-refcount object.
+  nsMappedAttributes*
+  GetModifiableMapped(nsMappedAttributeElement* aContent,
+                      nsHTMLStyleSheet* aSheet,
+                      bool aWillAddAttr);
   nsresult MakeMappedUnique(nsMappedAttributes* aAttributes);
 
   PRUint32 AttrSlotsSize() const
   {
     return AttrSlotCount() * ATTRSIZE;
   }
 
   PRUint32 AttrSlotCount() const
--- a/content/base/src/nsMappedAttributes.cpp
+++ b/content/base/src/nsMappedAttributes.cpp
@@ -63,19 +63,17 @@ void* nsMappedAttributes::operator new(s
 {
   NS_ASSERTION(aAttrCount > 0, "zero-attribute nsMappedAttributes requested");
 
   // aSize will include the mAttrs buffer so subtract that.
   void* newAttrs = ::operator new(aSize - sizeof(void*[1]) +
                                   aAttrCount * sizeof(InternalAttr));
 
 #ifdef DEBUG
-  if (newAttrs) {
-    static_cast<nsMappedAttributes*>(newAttrs)->mBufferSize = aAttrCount;
-  }
+  static_cast<nsMappedAttributes*>(newAttrs)->mBufferSize = aAttrCount;
 #endif
 
   return newAttrs;
 }
 
 NS_IMPL_ISUPPORTS1(nsMappedAttributes,
                    nsIStyleRule)
 
--- a/content/base/src/nsMappedAttributes.h
+++ b/content/base/src/nsMappedAttributes.h
@@ -21,18 +21,18 @@ class nsHTMLStyleSheet;
 class nsRuleWalker;
 
 class nsMappedAttributes MOZ_FINAL : public nsIStyleRule
 {
 public:
   nsMappedAttributes(nsHTMLStyleSheet* aSheet,
                      nsMapRuleToAttributesFunc aMapRuleFunc);
 
+  // Do not return null.
   void* operator new(size_t size, PRUint32 aAttrCount = 1) CPP_THROW_NEW;
-
   nsMappedAttributes* Clone(bool aWillAddAttr);
 
   NS_DECL_ISUPPORTS
 
   nsresult SetAndTakeAttr(nsIAtom* aAttrName, nsAttrValue& aValue);
   const nsAttrValue* GetAttr(nsIAtom* aAttrName) const;
   const nsAttrValue* GetAttr(const nsAString& aAttrName) const;