Bug 1316206 - Make RefPtr(decltype(nullptr)) MOZ_IMPLICIT - r=froydnj draft
authorGerald Squelart <gsquelart@mozilla.com>
Tue, 08 Nov 2016 15:12:33 +1100
changeset 436878 71d5dacac75ca188e5c55d45f48a5fca76d953c6
parent 436877 bc4107ce1767329e3dddfe34c55ba79202192e06
child 437113 cb8c508881c83f255ff255cd8103ada5349ded07
push id35228
push usergsquelart@mozilla.com
push dateWed, 09 Nov 2016 22:39:42 +0000
reviewersfroydnj
bugs1316206
milestone52.0a1
Bug 1316206 - Make RefPtr(decltype(nullptr)) MOZ_IMPLICIT - r=froydnj Making this constructor non-explicit will permit automatic conversions from 'nullptr' into RefPtr types, which I think are not dangerous. The one spot that this affects is in 'UserDataType nsBaseHashtable::Get(KeyType)', which does a 'return 0;' into the UserDataType, which could be a bool, an int, a RefPtr or other. I'm changing that into a C++11 "value initialization", which falls back to "zero initialization" for PODs: 'return UserDataType{};'. Also fixed the comment to clarify not-found return values, as Get(KeyType) was not only used for pointers anyway. MozReview-Commit-ID: F41VlvTNOZU
mfbt/RefPtr.h
xpcom/glue/nsBaseHashtable.h
--- a/mfbt/RefPtr.h
+++ b/mfbt/RefPtr.h
@@ -107,17 +107,17 @@ public:
   MOZ_IMPLICIT RefPtr(T* aRawPtr)
     : mRawPtr(aRawPtr)
   {
     if (mRawPtr) {
       ConstRemovingRefPtrTraits<T>::AddRef(mRawPtr);
     }
   }
 
-  explicit RefPtr(decltype(nullptr))
+  MOZ_IMPLICIT RefPtr(decltype(nullptr))
     : mRawPtr(nullptr)
   {
   }
 
   template <typename I>
   MOZ_IMPLICIT RefPtr(already_AddRefed<I>& aSmartPtr)
     : mRawPtr(aSmartPtr.take())
     // construct from |already_AddRefed|
--- a/xpcom/glue/nsBaseHashtable.h
+++ b/xpcom/glue/nsBaseHashtable.h
@@ -91,29 +91,30 @@ public:
     if (aData) {
       *aData = ent->mData;
     }
 
     return true;
   }
 
   /**
-   * For pointer types, get the value, returning nullptr if the entry is not
-   * present in the table.
+   * Get the value, returning a zero-initialized POD or a default-initialized
+   * object if the entry is not present in the table.
    *
    * @param aKey the key to retrieve
-   * @return The found value, or nullptr if no entry was found with the given key.
-   * @note If nullptr values are stored in the table, it is not possible to
-   *       distinguish between a nullptr value and a missing entry.
+   * @return The found value, or UserDataType{} if no entry was found with the
+   *         given key.
+   * @note If zero/default-initialized values are stored in the table, it is
+   *       not possible to distinguish between such a value and a missing entry.
    */
   UserDataType Get(KeyType aKey) const
   {
     EntryType* ent = this->GetEntry(aKey);
     if (!ent) {
-      return 0;
+      return UserDataType{};
     }
 
     return ent->mData;
   }
 
   /**
    * put a new value for the associated key
    * @param aKey the key to put