Bug 1493292 - GetStringSize instead of aDataLen. r=mats

Differential Revision: https://phabricator.services.mozilla.com/D11200

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Tom Schuster 2018-11-20 14:45:27 +00:00
Родитель 5a990aab93
Коммит b21e106022
2 изменённых файлов: 32 добавлений и 23 удалений

Просмотреть файл

@ -58,22 +58,32 @@ DataStruct::~DataStruct()
} }
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
void void
DataStruct::SetData(nsISupports* aData, uint32_t aDataLen, bool aIsPrivateData) DataStruct::SetData(nsISupports* aData, uint32_t aDataLen, bool aIsPrivateData)
{ {
// Now, check to see if we consider the data to be "too large" // Now, check to see if we consider the data to be "too large"
// as well as ensuring that private browsing mode is disabled // as well as ensuring that private browsing mode is disabled.
if (aDataLen > kLargeDatasetSize && !aIsPrivateData && // File IO is not allowed in content processes.
// File IO is not allowed in content processes. if (!aIsPrivateData && XRE_IsParentProcess()) {
XRE_IsParentProcess()) { void* data = nullptr;
// if so, cache it to disk instead of memory uint32_t dataLen = 0;
if (NS_SUCCEEDED(WriteCache(aData, aDataLen))) { nsPrimitiveHelpers::CreateDataFromPrimitive(mFlavor, aData, &data, &dataLen);
// Clear previously set small data.
mData = nullptr; if (dataLen > kLargeDatasetSize) {
mDataLen = 0; // Too large, cache it to disk instead of memory.
return; if (NS_SUCCEEDED(WriteCache(data, dataLen))) {
free(data);
// Clear previously set small data.
mData = nullptr;
mDataLen = 0;
return;
}
NS_WARNING("Oh no, couldn't write data to the cache file");
} }
NS_WARNING("Oh no, couldn't write data to the cache file");
free(data);
} }
if (mCacheFD) { if (mCacheFD) {
@ -114,8 +124,12 @@ DataStruct::GetData(nsISupports** aData, uint32_t* aDataLen)
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
nsresult nsresult
DataStruct::WriteCache(nsISupports* aData, uint32_t aDataLen) DataStruct::WriteCache(void* aData, uint32_t aDataLen)
{ {
MOZ_ASSERT(aData && aDataLen);
MOZ_ASSERT(aDataLen <= std::numeric_limits<int32_t>::max(),
"too large size for PR_Write");
nsresult rv; nsresult rv;
if (!mCacheFD) { if (!mCacheFD) {
rv = NS_OpenAnonymousTemporaryFile(&mCacheFD); rv = NS_OpenAnonymousTemporaryFile(&mCacheFD);
@ -126,17 +140,12 @@ DataStruct::WriteCache(nsISupports* aData, uint32_t aDataLen)
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
// write out the contents of the clipboard to the file // Write out the contents of the clipboard to the file.
void* buff = nullptr; int32_t written = PR_Write(mCacheFD, aData, aDataLen);
uint32_t dataLen = 0; if (written == aDataLen) {
nsPrimitiveHelpers::CreateDataFromPrimitive(mFlavor, aData, &buff, &dataLen); return NS_OK;
if (buff) {
int32_t written = PR_Write(mCacheFD, buff, dataLen);
free(buff);
if (written) {
return NS_OK;
}
} }
PR_Close(mCacheFD); PR_Close(mCacheFD);
mCacheFD = nullptr; mCacheFD = nullptr;
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;

Просмотреть файл

@ -42,7 +42,7 @@ protected:
kLargeDatasetSize = 1000000 // 1 million bytes kLargeDatasetSize = 1000000 // 1 million bytes
}; };
nsresult WriteCache(nsISupports* aData, uint32_t aDataLen ); nsresult WriteCache(void* aData, uint32_t aDataLen);
nsresult ReadCache(nsISupports** aData, uint32_t* aDataLen ); nsresult ReadCache(nsISupports** aData, uint32_t* aDataLen );
// mData + mDataLen OR mCacheFD should be used, not both. // mData + mDataLen OR mCacheFD should be used, not both.