@@ -704,32 +704,34 @@ 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. 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.
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.
712711template <typename CharType>
713712static nsresult GetCharDataFromGlobalData (STGMEDIUM& aStm, CharType** aData,
714713 uint32_t * aByteLen) {
715714 uint32_t nBytes = 0 ;
716715 MOZ_TRY (nsClipboard::GetGlobalData (aStm.hGlobal ,
717716 reinterpret_cast <void **>(aData), &nBytes));
718717 auto nChars = nBytes / sizeof (CharType);
718+ if (nChars < 1 ) {
719+ *aByteLen = 0 ;
720+ return NS_OK;
721+ }
719722
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 )) {
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.
725728 nChars -= 2 ;
729+ } else if (data[nChars - 1 ] == CharType (0 )) {
730+ // Remove null termination.
731+ nChars -= 1 ;
726732 }
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);
733+ *aByteLen = nChars * sizeof (CharType);
734+
733735 return NS_OK;
734736}
735737
0 commit comments