Skip to content

Commit e7e395f

Browse files
davidp3DonalMe
authored andcommitted
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
1 parent c04413f commit e7e395f

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

widget/windows/nsClipboard.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -704,34 +704,32 @@ HRESULT nsClipboard::FillSTGMedium(IDataObject* aDataObject, UINT aFormat,
704704
// Get the data out of the global data handle. The size we
705705
// return should be the size returned from GetGlobalData(), since the
706706
// string returned from Windows may have nulls inserted -- it may not
707-
// even be null-terminated. GetGlobalData adds bytes for null
708-
// termination to the buffer but they are not considered in the returned
709-
// byte count. We check and skip the last counted byte if it is a null
710-
// since Windows also appears to add null termination. See GetGlobalData.
707+
// even be null-terminated. This does not agree with the documentation of
708+
// CF_TEXT/CF_UNICODETEXT but it is reality. GetGlobalData
709+
// adds bytes for null termination to the buffer but they are not considered
710+
// in the returned byte count. We check and skip bytes based on the policy
711+
// described below. See also GetGlobalData.
711712
template <typename CharType>
712713
static nsresult GetCharDataFromGlobalData(STGMEDIUM& aStm, CharType** aData,
713714
uint32_t* aByteLen) {
714715
uint32_t nBytes = 0;
715716
MOZ_TRY(nsClipboard::GetGlobalData(aStm.hGlobal,
716717
reinterpret_cast<void**>(aData), &nBytes));
717718
auto nChars = nBytes / sizeof(CharType);
718-
if (nChars < 1) {
719-
*aByteLen = 0;
720-
return NS_OK;
721-
}
722719

723-
const CharType* data = *aData;
724-
if (nChars > 1 && data[nChars - 2] == CharType(0x00) &&
725-
data[nChars - 1] == CharType(0x0a)) {
726-
// The char array ends in the nonsense combination null + LF. Remove both.
727-
// Word sometimes does this.
720+
// Word sometimes adds a null, then a LF (0x0a), to the end of strings.
721+
// Special case their removal.
722+
if (nChars > 1 &&
723+
(*aData)[nChars-2] == CharType(0) &&
724+
(*aData)[nChars-1] == CharType(0xa)) {
728725
nChars -= 2;
729-
} else if (data[nChars - 1] == CharType(0)) {
730-
// Remove null termination.
731-
nChars -= 1;
732726
}
733-
*aByteLen = nChars * sizeof(CharType);
734-
727+
// Remove any nulls that appear at the end of the string.
728+
CharType* afterLastChar = *aData + nChars;
729+
auto it = std::find_if(
730+
std::reverse_iterator(afterLastChar), std::reverse_iterator(*aData),
731+
[](CharType ch) { return ch != CharType(0); });
732+
*aByteLen = std::distance(*aData, it.base()) * sizeof(CharType);
735733
return NS_OK;
736734
}
737735

0 commit comments

Comments
 (0)