From 3a8e0049b7b3286e2a97841a4e0ba0cc3d6f326e Mon Sep 17 00:00:00 2001 From: Tom Schuster Date: Fri, 9 Nov 2018 13:24:28 +0000 Subject: [PATCH] Bug 1503577 - Change CreateDataFromPrimitive aDataLen to out parameter with size of string in bytes. r=mstange Right now CreateDataFromPrimitive doesn't use the aDataLen parameter, and after this change the out value should be the same as the value passed in, as long as we are dealing with strings. Differential Revision: https://phabricator.services.mozilla.com/D10718 --HG-- extra : moz-landing-system : lando --- widget/gtk/nsClipboard.cpp | 5 +++-- widget/gtk/nsDragService.cpp | 10 +++++----- widget/nsPrimitiveHelpers.cpp | 7 +++++-- widget/nsPrimitiveHelpers.h | 4 ++-- widget/nsTransferable.cpp | 5 +++-- widget/windows/nsDataObj.cpp | 2 +- 6 files changed, 19 insertions(+), 14 deletions(-) diff --git a/widget/gtk/nsClipboard.cpp b/widget/gtk/nsClipboard.cpp index bc9897b30ab9..5586eff96840 100644 --- a/widget/gtk/nsClipboard.cpp +++ b/widget/gtk/nsClipboard.cpp @@ -570,13 +570,14 @@ nsClipboard::SelectionGetEvent(GtkClipboard *aClipboard, } void *primitive_data = nullptr; + uint32_t dataLen = 0; nsPrimitiveHelpers::CreateDataFromPrimitive(nsDependentCString(target_name), - item, &primitive_data, len); + item, &primitive_data, &dataLen); if (primitive_data) { gtk_selection_data_set(aSelectionData, selectionTarget, 8, /* 8 bits in a unit */ - (const guchar *)primitive_data, len); + (const guchar *)primitive_data, dataLen); free(primitive_data); } diff --git a/widget/gtk/nsDragService.cpp b/widget/gtk/nsDragService.cpp index 820243339c6a..e8405fbf7b1e 100644 --- a/widget/gtk/nsDragService.cpp +++ b/widget/gtk/nsDragService.cpp @@ -1675,14 +1675,12 @@ nsDragService::SourceDataGet(GtkWidget *aWidget, } else { actualFlavor = mimeFlavor.get(); } - - uint32_t tmpDataLen = 0; - void *tmpData = nullptr; nsresult rv; nsCOMPtr data; + uint32_t len; rv = item->GetTransferData(actualFlavor, getter_AddRefs(data), - &tmpDataLen); + &len); if (strcmp(actualFlavor, kFilePromiseMime) == 0) { if (NS_SUCCEEDED(rv)) { @@ -1694,8 +1692,10 @@ nsDragService::SourceDataGet(GtkWidget *aWidget, } if (NS_SUCCEEDED(rv)) { + void *tmpData = nullptr; + uint32_t tmpDataLen = 0; nsPrimitiveHelpers::CreateDataFromPrimitive( - nsDependentCString(actualFlavor), data, &tmpData, tmpDataLen); + nsDependentCString(actualFlavor), data, &tmpData, &tmpDataLen); // if required, do the extra work to convert unicode to plain // text and replace the output values with the plain text. if (needToDoConversionToPlainText) { diff --git a/widget/nsPrimitiveHelpers.cpp b/widget/nsPrimitiveHelpers.cpp index f98ead28d6a6..2157621e06bc 100644 --- a/widget/nsPrimitiveHelpers.cpp +++ b/widget/nsPrimitiveHelpers.cpp @@ -125,13 +125,14 @@ nsPrimitiveHelpers :: CreatePrimitiveForCFHTML ( const void* aDataBuff, // parameter does not reflect that. // void -nsPrimitiveHelpers :: CreateDataFromPrimitive ( const nsACString& aFlavor, nsISupports* aPrimitive, - void** aDataBuff, uint32_t aDataLen ) +nsPrimitiveHelpers::CreateDataFromPrimitive(const nsACString& aFlavor, nsISupports* aPrimitive, + void** aDataBuff, uint32_t* aDataLen) { if ( !aDataBuff ) return; *aDataBuff = nullptr; + *aDataLen = 0; if (aFlavor.EqualsLiteral(kTextMime) || aFlavor.EqualsLiteral(kCustomTypesMime)) { @@ -140,6 +141,7 @@ nsPrimitiveHelpers :: CreateDataFromPrimitive ( const nsACString& aFlavor, nsISu nsAutoCString data; plainText->GetData ( data ); *aDataBuff = ToNewCString(data); + *aDataLen = data.Length() * sizeof(char); } } else { @@ -148,6 +150,7 @@ nsPrimitiveHelpers :: CreateDataFromPrimitive ( const nsACString& aFlavor, nsISu nsAutoString data; doubleByteText->GetData ( data ); *aDataBuff = ToNewUnicode(data); + *aDataLen = data.Length() * sizeof(char16_t); } } diff --git a/widget/nsPrimitiveHelpers.h b/widget/nsPrimitiveHelpers.h index 4a1455b07f58..7d8cbf6a3029 100644 --- a/widget/nsPrimitiveHelpers.h +++ b/widget/nsPrimitiveHelpers.h @@ -31,8 +31,8 @@ public: // Given a nsISupports* primitive and the flavor it represents, creates a new data // buffer with the data in it. This data will be null terminated, but the length // parameter does not reflect that. - static void CreateDataFromPrimitive ( const nsACString& aFlavor, nsISupports* aPrimitive, - void** aDataBuff, uint32_t aDataLen ) ; + static void CreateDataFromPrimitive(const nsACString& aFlavor, nsISupports* aPrimitive, + void** aDataBuff, uint32_t* aDataLen); }; // class nsPrimitiveHelpers diff --git a/widget/nsTransferable.cpp b/widget/nsTransferable.cpp index b8822155cbe7..c67923d98500 100644 --- a/widget/nsTransferable.cpp +++ b/widget/nsTransferable.cpp @@ -126,9 +126,10 @@ DataStruct::WriteCache(nsISupports* aData, uint32_t aDataLen) // write out the contents of the clipboard to the file void* buff = nullptr; - nsPrimitiveHelpers::CreateDataFromPrimitive(mFlavor, aData, &buff, aDataLen); + uint32_t dataLen = 0; + nsPrimitiveHelpers::CreateDataFromPrimitive(mFlavor, aData, &buff, &dataLen); if (buff) { - int32_t written = PR_Write(mCacheFD, buff, aDataLen); + int32_t written = PR_Write(mCacheFD, buff, dataLen); free(buff); if (written) { return NS_OK; diff --git a/widget/windows/nsDataObj.cpp b/widget/windows/nsDataObj.cpp index 555f4d5c2efe..a0238d8b6fbe 100644 --- a/widget/windows/nsDataObj.cpp +++ b/widget/windows/nsDataObj.cpp @@ -1339,7 +1339,7 @@ HRESULT nsDataObj::GetText(const nsACString & aDataFlavor, FORMATETC& aFE, STGME if ( !len ) return E_FAIL; nsPrimitiveHelpers::CreateDataFromPrimitive( - nsDependentCString(flavorStr), genericDataWrapper, &data, len); + nsDependentCString(flavorStr), genericDataWrapper, &data, &len); if ( !data ) return E_FAIL;