Bug 1969820: Remove 0x00 0x0a and trailing nulls from clipboard a=dmeehan

We previously removed (0x00 0x0a|0x00)?.  We now remove (0x00)*(0x00 0x0a)?.

Original Revision: https://phabricator.services.mozilla.com/D252588

Differential Revision: https://phabricator.services.mozilla.com/D253178
This commit is contained in:
David P
2025-06-10 17:06:47 +00:00
committed by dmeehan@mozilla.com
parent c04413f815
commit e7e395fb0a

View File

@@ -704,10 +704,11 @@ HRESULT nsClipboard::FillSTGMedium(IDataObject* aDataObject, UINT aFormat,
// Get the data out of the global data handle. The size we // Get the data out of the global data handle. The size we
// return should be the size returned from GetGlobalData(), since the // return should be the size returned from GetGlobalData(), since the
// string returned from Windows may have nulls inserted -- it may not // string returned from Windows may have nulls inserted -- it may not
// even be null-terminated. GetGlobalData adds bytes for null // even be null-terminated. This does not agree with the documentation of
// termination to the buffer but they are not considered in the returned // CF_TEXT/CF_UNICODETEXT but it is reality. GetGlobalData
// byte count. We check and skip the last counted byte if it is a null // adds bytes for null termination to the buffer but they are not considered
// since Windows also appears to add null termination. See GetGlobalData. // in the returned byte count. We check and skip bytes based on the policy
// described below. See also GetGlobalData.
template <typename CharType> template <typename CharType>
static nsresult GetCharDataFromGlobalData(STGMEDIUM& aStm, CharType** aData, static nsresult GetCharDataFromGlobalData(STGMEDIUM& aStm, CharType** aData,
uint32_t* aByteLen) { uint32_t* aByteLen) {
@@ -715,23 +716,20 @@ static nsresult GetCharDataFromGlobalData(STGMEDIUM& aStm, CharType** aData,
MOZ_TRY(nsClipboard::GetGlobalData(aStm.hGlobal, MOZ_TRY(nsClipboard::GetGlobalData(aStm.hGlobal,
reinterpret_cast<void**>(aData), &nBytes)); reinterpret_cast<void**>(aData), &nBytes));
auto nChars = nBytes / sizeof(CharType); auto nChars = nBytes / sizeof(CharType);
if (nChars < 1) {
*aByteLen = 0;
return NS_OK;
}
const CharType* data = *aData; // Word sometimes adds a null, then a LF (0x0a), to the end of strings.
if (nChars > 1 && data[nChars - 2] == CharType(0x00) && // Special case their removal.
data[nChars - 1] == CharType(0x0a)) { if (nChars > 1 &&
// The char array ends in the nonsense combination null + LF. Remove both. (*aData)[nChars-2] == CharType(0) &&
// Word sometimes does this. (*aData)[nChars-1] == CharType(0xa)) {
nChars -= 2; nChars -= 2;
} else if (data[nChars - 1] == CharType(0)) {
// Remove null termination.
nChars -= 1;
} }
*aByteLen = nChars * sizeof(CharType); // Remove any nulls that appear at the end of the string.
CharType* afterLastChar = *aData + nChars;
auto it = std::find_if(
std::reverse_iterator(afterLastChar), std::reverse_iterator(*aData),
[](CharType ch) { return ch != CharType(0); });
*aByteLen = std::distance(*aData, it.base()) * sizeof(CharType);
return NS_OK; return NS_OK;
} }