From b7be7bd4a64fbcee890bc65ef462b4f22ff945ef Mon Sep 17 00:00:00 2001 From: DonalMe <76789979+DonalMe@users.noreply.github.com> Date: Thu, 12 Jun 2025 10:13:16 -0400 Subject: [PATCH] Revert "Bug 1966443: Use proper length for Windows clipboard elements r=win-reviewers,emilio,gstoll" for causing Bug 1968744 This reverts commit cff2580bc6b540ff1bb9b890d9cf79c1bcc563a5. --- widget/windows/nsClipboard.cpp | 50 +++++++++++++++------------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/widget/windows/nsClipboard.cpp b/widget/windows/nsClipboard.cpp index dc272f1ff856..1a68aa7e39e2 100644 --- a/widget/windows/nsClipboard.cpp +++ b/widget/windows/nsClipboard.cpp @@ -578,7 +578,6 @@ NS_IMETHODIMP nsClipboard::SetNativeClipboardData( } //------------------------------------------------------------------------- -/* static */ nsresult nsClipboard::GetGlobalData(HGLOBAL aHGBL, void** aData, uint32_t* aLen) { MOZ_CLIPBOARD_LOG("%s", __FUNCTION__); @@ -701,29 +700,6 @@ HRESULT nsClipboard::FillSTGMedium(IDataObject* aDataObject, UINT aFormat, } //------------------------------------------------------------------------- -// Get the data out of the global data handle. The size we -// return should be the size returned from GetGlobalData(), since the -// string returned from Windows may have nulls inserted -- it may not -// even be null-terminated. GetGlobalData adds bytes for null -// termination to the buffer but they are not considered in the returned -// byte count. We check and skip the last counted byte if it is a null -// since Windows also appears to add null termination. See GetGlobalData. -template -static nsresult GetCharDataFromGlobalData(STGMEDIUM& aStm, CharType** aData, - uint32_t* aLen) { - uint32_t nBytes = 0; - MOZ_TRY(nsClipboard::GetGlobalData(aStm.hGlobal, - reinterpret_cast(aData), &nBytes)); - auto nChars = nBytes / sizeof(CharType); - if (nChars < 1) { - *aLen = 0; - return NS_OK; - } - bool hasNullTerminator = (*aData)[nChars - 1] == CharType(0); - *aLen = hasNullTerminator ? nBytes - sizeof(CharType) : nBytes; - return NS_OK; -} - // If aFormat is CF_DIBV5, aMIMEImageFormat must be a type for which we have // an image encoder (e.g. image/png). // For other values of aFormat, it is OK to pass null for aMIMEImageFormat. @@ -788,13 +764,31 @@ nsresult nsClipboard::GetNativeDataOffClipboard(IDataObject* aDataObject, // compile-time-constant format indicators: switch (fe.cfFormat) { case CF_TEXT: { - return GetCharDataFromGlobalData(stm, reinterpret_cast(aData), - aLen); + // Get the data out of the global data handle. The size we + // return should not include the null because the other + // platforms don't use nulls, so just return the length we get + // back from strlen(), since we know CF_TEXT is null + // terminated. Recall that GetGlobalData() returns the size of + // the allocated buffer, not the size of the data (on 98, these + // are not the same) so we can't use that. + uint32_t allocLen = 0; + MOZ_TRY(GetGlobalData(stm.hGlobal, aData, &allocLen)); + *aLen = strlen(reinterpret_cast(*aData)); + return NS_OK; } case CF_UNICODETEXT: { - return GetCharDataFromGlobalData(stm, reinterpret_cast(aData), - aLen); + // Get the data out of the global data handle. The size we + // return should not include the null because the other + // platforms don't use nulls, so just return the length we get + // back from strlen(), since we know CF_UNICODETEXT is null + // terminated. Recall that GetGlobalData() returns the size of + // the allocated buffer, not the size of the data (on 98, these + // are not the same) so we can't use that. + uint32_t allocLen = 0; + MOZ_TRY(GetGlobalData(stm.hGlobal, aData, &allocLen)); + *aLen = NS_strlen(reinterpret_cast(*aData)) * 2; + return NS_OK; } case CF_DIBV5: {