Bug 335545 - Make DataStruct non-copyable r=mstange
authorRob Wu <rob@robwu.nl>
Sun, 25 Feb 2018 17:36:08 +0100
changeset 461637 5469af7e5a36d3c34b876ba6fccf17647bc258a7
parent 461636 a9eca77bd638babb8de38824f1e61b4bbd8424f6
child 461638 46c607ac246a3a0c16d190a9c4f8cd41d38a945a
push id1683
push usersfraser@mozilla.com
push dateThu, 26 Apr 2018 16:43:40 +0000
treeherdermozilla-release@5af6cb21869d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmstange
bugs335545
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 335545 - Make DataStruct non-copyable r=mstange DataStruct cannot safely be copied if its mCacheFD member is set. Currently there is no code for this case, but to avoid problems later, mark the copy and assignment constructors private and delete them. A move-constructor was added to compensate for the deleted copy constructor. nsTransferable::AddDataFlavor uses this new constructor instead of the previous implicit default copy constructor. MozReview-Commit-ID: 3N5xjFXOUKB
widget/nsTransferable.cpp
widget/nsTransferable.h
--- a/widget/nsTransferable.cpp
+++ b/widget/nsTransferable.cpp
@@ -47,16 +47,25 @@ size_t GetDataForFlavor (const nsTArray<
   for (size_t i = 0 ; i < aArray.Length () ; ++i) {
     if (aArray[i].GetFlavor().Equals (aDataFlavor))
       return i;
   }
 
   return aArray.NoIndex;
 }
 
+DataStruct::DataStruct(DataStruct&& aRHS)
+  : mData(aRHS.mData.forget()),
+    mDataLen(aRHS.mDataLen),
+    mCacheFD(aRHS.mCacheFD),
+    mFlavor(aRHS.mFlavor)
+{
+  aRHS.mCacheFD = nullptr;
+}
+
 //-------------------------------------------------------------------------
 DataStruct::~DataStruct()
 {
   if (mCacheFD) {
     PR_Close(mCacheFD);
   }
 }
 
--- a/widget/nsTransferable.h
+++ b/widget/nsTransferable.h
@@ -20,16 +20,17 @@ class nsIMutableArray;
 // DataStruct
 //
 // Holds a flavor (a mime type) that describes the data and the associated data.
 //
 struct DataStruct
 {
   explicit DataStruct ( const char* aFlavor )
     : mDataLen(0), mCacheFD(nullptr), mFlavor(aFlavor) { }
+  DataStruct(DataStruct&& aRHS);
   ~DataStruct();
   
   const nsCString& GetFlavor() const { return mFlavor; }
   void SetData( nsISupports* inData, uint32_t inDataLen, bool aIsPrivateData );
   void GetData( nsISupports** outData, uint32_t *outDataLen );
   bool IsDataAvailable() const { return mData ? mDataLen > 0 : mCacheFD != nullptr; }
   
 protected:
@@ -44,16 +45,20 @@ protected:
   nsresult ReadCache(nsISupports** aData, uint32_t* aDataLen );
   
   // mData + mDataLen OR mCacheFD should be used, not both.
   nsCOMPtr<nsISupports> mData;   // OWNER - some varient of primitive wrapper
   uint32_t mDataLen;
   PRFileDesc* mCacheFD;
   const nsCString mFlavor;
 
+private:
+  DataStruct(const DataStruct&) = delete;
+  DataStruct& operator=(const DataStruct&) = delete;
+
 };
 
 /**
  * XP Transferable wrapper
  */
 
 class nsTransferable : public nsITransferable
 {