Skip to content

Commit c94e6fe

Browse files
committed
Revert "Bug 1966443: Use proper length for Windows clipboard elements r=win-reviewers,emilio,gstoll" for causing Bug 1968744
This reverts commit cff2580.
1 parent 62baff5 commit c94e6fe

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

widget/windows/nsClipboard.cpp

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,6 @@ NS_IMETHODIMP nsClipboard::SetNativeClipboardData(
578578
}
579579

580580
//-------------------------------------------------------------------------
581-
/* static */
582581
nsresult nsClipboard::GetGlobalData(HGLOBAL aHGBL, void** aData,
583582
uint32_t* aLen) {
584583
MOZ_CLIPBOARD_LOG("%s", __FUNCTION__);
@@ -701,29 +700,6 @@ HRESULT nsClipboard::FillSTGMedium(IDataObject* aDataObject, UINT aFormat,
701700
}
702701

703702
//-------------------------------------------------------------------------
704-
// Get the data out of the global data handle. The size we
705-
// return should be the size returned from GetGlobalData(), since the
706-
// 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.
711-
template <typename CharType>
712-
static nsresult GetCharDataFromGlobalData(STGMEDIUM& aStm, CharType** aData,
713-
uint32_t* aLen) {
714-
uint32_t nBytes = 0;
715-
MOZ_TRY(nsClipboard::GetGlobalData(aStm.hGlobal,
716-
reinterpret_cast<void**>(aData), &nBytes));
717-
auto nChars = nBytes / sizeof(CharType);
718-
if (nChars < 1) {
719-
*aLen = 0;
720-
return NS_OK;
721-
}
722-
bool hasNullTerminator = (*aData)[nChars - 1] == CharType(0);
723-
*aLen = hasNullTerminator ? nBytes - sizeof(CharType) : nBytes;
724-
return NS_OK;
725-
}
726-
727703
// If aFormat is CF_DIBV5, aMIMEImageFormat must be a type for which we have
728704
// an image encoder (e.g. image/png).
729705
// For other values of aFormat, it is OK to pass null for aMIMEImageFormat.
@@ -788,13 +764,31 @@ nsresult nsClipboard::GetNativeDataOffClipboard(IDataObject* aDataObject,
788764
// compile-time-constant format indicators:
789765
switch (fe.cfFormat) {
790766
case CF_TEXT: {
791-
return GetCharDataFromGlobalData(stm, reinterpret_cast<char**>(aData),
792-
aLen);
767+
// Get the data out of the global data handle. The size we
768+
// return should not include the null because the other
769+
// platforms don't use nulls, so just return the length we get
770+
// back from strlen(), since we know CF_TEXT is null
771+
// terminated. Recall that GetGlobalData() returns the size of
772+
// the allocated buffer, not the size of the data (on 98, these
773+
// are not the same) so we can't use that.
774+
uint32_t allocLen = 0;
775+
MOZ_TRY(GetGlobalData(stm.hGlobal, aData, &allocLen));
776+
*aLen = strlen(reinterpret_cast<char*>(*aData));
777+
return NS_OK;
793778
}
794779

795780
case CF_UNICODETEXT: {
796-
return GetCharDataFromGlobalData(stm, reinterpret_cast<char16_t**>(aData),
797-
aLen);
781+
// Get the data out of the global data handle. The size we
782+
// return should not include the null because the other
783+
// platforms don't use nulls, so just return the length we get
784+
// back from strlen(), since we know CF_UNICODETEXT is null
785+
// terminated. Recall that GetGlobalData() returns the size of
786+
// the allocated buffer, not the size of the data (on 98, these
787+
// are not the same) so we can't use that.
788+
uint32_t allocLen = 0;
789+
MOZ_TRY(GetGlobalData(stm.hGlobal, aData, &allocLen));
790+
*aLen = NS_strlen(reinterpret_cast<char16_t*>(*aData)) * 2;
791+
return NS_OK;
798792
}
799793

800794
case CF_DIBV5: {

0 commit comments

Comments
 (0)