VirtualBox

Changeset 83630 in vbox for trunk/src/VBox/HostServices


Ignore:
Timestamp:
Apr 8, 2020 7:13:56 PM (5 years ago)
Author:
vboxsync
Message:

SharedClipboard: Fixed leaks in sloppy error handling in writeToPasteboard. bugref:9620

Location:
trunk/src/VBox/HostServices/SharedClipboard
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostServices/SharedClipboard/darwin-pasteboard.cpp

    r83628 r83630  
    344344 * structure.
    345345 *
    346  * @param   pPasteboard    Reference to the global pasteboard.
     346 * @param   hPasteboard    Reference to the global pasteboard.
    347347 * @param   idOwnership    The ownership ID.
    348348 * @param   pv             The source buffer.
     
    352352 * @returns IPRT status code.
    353353 */
    354 int writeToPasteboard(PasteboardRef pPasteboard, uint64_t idOwnership, void *pv, uint32_t cb, uint32_t fFormat)
    355 {
     354int writeToPasteboard(PasteboardRef hPasteboard, uint64_t idOwnership, const void *pv, uint32_t cb, uint32_t fFormat)
     355{
     356    int rc;
     357    OSStatus orc;
    356358    Log(("writeToPasteboard: fFormat = %02X\n", fFormat));
    357359
    358360    /* Make sure all is in sync */
    359     PasteboardSynchronize(pPasteboard);
    360 
    361     int rc = VERR_NOT_SUPPORTED;
    362     /* Handle the unicode text */
     361    PasteboardSynchronize(hPasteboard);
     362
     363    /*
     364     * Handle the unicode text
     365     */
    363366    if (fFormat & VBOX_SHCL_FMT_UNICODETEXT)
    364367    {
    365         PRTUTF16 pwszSrcText = static_cast <PRTUTF16>(pv);
    366         size_t cwSrc = cb / 2;
    367         size_t cwDest = 0;
     368        PCRTUTF16 const pwszSrc = (PCRTUTF16)pv;
     369        size_t const    cwcSrc  = cb / sizeof(RTUTF16);
     370
     371        /*
     372         * If the other side is windows or OS/2, we may have to convert
     373         * '\r\n' -> '\n' and the drop ending marker.
     374         */
     375
    368376        /* How long will the converted text be? */
    369         rc = ShClUtf16GetLinSize(pwszSrcText, cwSrc, &cwDest);
    370         if (RT_FAILURE(rc))
    371         {
    372             Log(("writeToPasteboard: clipboard conversion failed.  vboxClipboardUtf16GetLinSize returned %Rrc.  Abandoning.\n", rc));
    373             AssertRCReturn(rc, rc);
    374         }
    375         /* Empty clipboard? Not critical */
    376         if (cwDest == 0)
    377         {
    378             Log(("writeToPasteboard: received empty clipboard data from the guest, returning false.\n"));
     377        size_t cwcDst = 0;
     378        rc = ShClUtf16GetLinSize(pwszSrc, cwcSrc, &cwcDst);
     379        AssertMsgRCReturn(rc, ("ShClUtf16GetLinSize failed: %Rrc\n", rc), rc);
     380
     381        /* Ignore empty strings? */
     382        if (cwcDst == 0)
     383        {
     384            Log(("writeToPasteboard: received empty string from the guest; ignoreing it.\n"));
    379385            return VINF_SUCCESS;
    380386        }
    381         /* Allocate the necessary memory */
    382         PRTUTF16 pwszDestText = static_cast <PRTUTF16>(RTMemAlloc(cwDest * 2));
    383         if (pwszDestText == NULL)
    384         {
    385             Log(("writeToPasteboard: failed to allocate %d bytes\n", cwDest * 2));
    386             return VERR_NO_MEMORY;
    387         }
    388         /* Convert the EOL */
    389         rc = ShClUtf16WinToLin(pwszSrcText, cwSrc, pwszDestText, cwDest);
    390         if (RT_FAILURE(rc))
    391         {
     387
     388        /* Allocate the necessary memory and do the conversion. */
     389        PRTUTF16 pwszDst = (PRTUTF16)RTMemAlloc(cwcDst * sizeof(RTUTF16));
     390        AssertMsgReturn(pwszDst, ("cwcDst=%#zx\n", cwcDst), VERR_NO_UTF16_MEMORY);
     391
     392        rc = ShClUtf16WinToLin(pwszSrc, cwcSrc, pwszDst, cwcDst);
     393        if (RT_SUCCESS(rc))
     394        {
     395            /*
     396             * Create an immutable CFData object that we can place on the clipboard.
     397             */
     398            rc = VINF_SUCCESS;
     399            CFDataRef hData = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)pwszDst, cwcDst * sizeof(RTUTF16));
     400            if (hData)
     401            {
     402                orc = PasteboardPutItemFlavor(hPasteboard, (PasteboardItemID)idOwnership,
     403                                              kUTTypeUTF16PlainText, hData, kPasteboardFlavorNoFlags);
     404                if (orc != 0)
     405                {
     406                    Log(("writeToPasteboard: PasteboardPutItemFlavor/kUTTypeUTF16PlainText failed: %d (%#x)\n", orc, orc));
     407                    rc = VERR_GENERAL_FAILURE;
     408                }
     409                else
     410                {
     411                    Log(("writeToPasteboard: CFDataCreate/UTF16 failed!\n"));
     412                    rc = VERR_NO_MEMORY;
     413                }
     414                CFRelease(hData);
     415            }
     416
     417            /*
     418             * Now for the UTF-8 version.
     419             */
     420            char *pszDst;
     421            int rc2 = RTUtf16ToUtf8(pwszDst, &pszDst);
     422            if (RT_SUCCESS(rc2))
     423            {
     424                hData = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)pszDst, strlen(pszDst));
     425                if (hData)
     426                {
     427                    orc = PasteboardPutItemFlavor(hPasteboard, (PasteboardItemID)idOwnership,
     428                                                  kUTTypeUTF8PlainText, hData, kPasteboardFlavorNoFlags);
     429                    if (orc != 0)
     430                    {
     431                        Log(("writeToPasteboard: PasteboardPutItemFlavor/kUTTypeUTF8PlainText failed: %d (%#x)\n", orc, orc));
     432                        rc = VERR_GENERAL_FAILURE;
     433                    }
     434                    CFRelease(hData);
     435                }
     436                else
     437                {
     438                    Log(("writeToPasteboard: CFDataCreate/UTF8 failed!\n"));
     439                    rc = VERR_NO_MEMORY;
     440                }
     441                RTStrFree(pszDst);
     442            }
     443            else
     444                rc = rc2;
     445        }
     446        else
    392447            Log(("writeToPasteboard: clipboard conversion failed.  vboxClipboardUtf16WinToLin() returned %Rrc.  Abandoning.\n", rc));
    393             RTMemFree(pwszDestText);
    394             AssertRCReturn(rc, rc);
    395         }
    396 
    397         CFDataRef textData = NULL;
    398         /* Create a CData object which we could pass to the pasteboard */
    399         if ((textData = CFDataCreate(kCFAllocatorDefault,
    400                                      reinterpret_cast<UInt8*>(pwszDestText), cwDest * 2)))
    401         {
    402             /* Put the Utf-16 version to the pasteboard */
    403             PasteboardPutItemFlavor(pPasteboard, (PasteboardItemID)idOwnership, kUTTypeUTF16PlainText, textData, 0);
    404         }
    405         /* Create a Utf-8 version */
    406         char *pszDestText;
    407         rc = RTUtf16ToUtf8(pwszDestText, &pszDestText);
     448
     449        RTMemFree(pwszDst);
     450    }
     451    /*
     452     * Handle the bitmap.  We convert the DIB to a bitmap and put it on
     453     * the pasteboard using the BMP flavor.
     454     */
     455    else if (fFormat & VBOX_SHCL_FMT_BITMAP)
     456    {
     457        /* Create a full BMP from it */
     458        void  *pvBmp;
     459        size_t cbBmp;
     460        rc = ShClDibToBmp(pv, cb, &pvBmp, &cbBmp);
    408461        if (RT_SUCCESS(rc))
    409462        {
    410             /* Create a CData object which we could pass to the pasteboard */
    411             if ((textData = CFDataCreate(kCFAllocatorDefault,
    412                                          reinterpret_cast<UInt8*>(pszDestText), strlen(pszDestText))))
    413             {
    414                 /* Put the Utf-8 version to the pasteboard */
    415                 PasteboardPutItemFlavor(pPasteboard, (PasteboardItemID)idOwnership, kUTTypeUTF8PlainText, textData, 0);
    416             }
    417             RTStrFree(pszDestText);
    418         }
    419 
    420         RTMemFree(pwszDestText);
    421         rc = VINF_SUCCESS;
    422     }
    423     /* Handle the bitmap */
    424     else if (fFormat & VBOX_SHCL_FMT_BITMAP)
    425     {
    426         /* Create a full BMP from it */
    427         void *pBmp;
    428         size_t cbBmpSize;
    429         CFDataRef bmpData = NULL;
    430 
    431         rc = ShClDibToBmp(pv, cb, &pBmp, &cbBmpSize);
    432         if (RT_SUCCESS(rc))
    433         {
    434             /* Create a CData object which we could pass to the pasteboard */
    435             if ((bmpData = CFDataCreate(kCFAllocatorDefault,
    436                                          reinterpret_cast<UInt8*>(pBmp), cbBmpSize)))
    437             {
    438                 /* Put the Utf-8 version to the pasteboard */
    439                 PasteboardPutItemFlavor(pPasteboard, (PasteboardItemID)idOwnership, kUTTypeBMP, bmpData, 0);
    440             }
    441             RTMemFree(pBmp);
    442         }
    443         rc = VINF_SUCCESS;
     463            CFDataRef hData = CFDataCreate(kCFAllocatorDefault, (UInt8 const *)pvBmp, cbBmp);
     464            if (hData)
     465            {
     466                orc = PasteboardPutItemFlavor(hPasteboard, (PasteboardItemID)idOwnership,
     467                                              kUTTypeBMP, hData, kPasteboardFlavorNoFlags);
     468                if (orc != 0)
     469                {
     470                    Log(("writeToPasteboard: PasteboardPutItemFlavor/kUTTypeBMP failed: %d (%#x)\n", orc, orc));
     471                    rc = VERR_GENERAL_FAILURE;
     472                }
     473                CFRelease(hData);
     474            }
     475            else
     476            {
     477                Log(("writeToPasteboard: CFDataCreate/UTF8 failed!\n"));
     478                rc = VERR_NO_MEMORY;
     479            }
     480            RTMemFree(pvBmp);
     481        }
    444482    }
    445483    else
    446484        rc = VERR_NOT_IMPLEMENTED;
    447485
    448     Log(("writeToPasteboard: rc = %02X\n", rc));
     486    Log(("writeToPasteboard: rc=%Rrc\n", rc));
    449487    return rc;
    450488}
  • trunk/src/VBox/HostServices/SharedClipboard/darwin-pasteboard.h

    r83628 r83630  
    3131int takePasteboardOwnership(PasteboardRef pPasteboard, uint64_t idOwnership,
    3232                            const char *pszOwnershipFlavor, const char *pszOwnershipValue);
    33 int writeToPasteboard(PasteboardRef pPasteboard, uint64_t idOwnership, void *pv, uint32_t cb, uint32_t fFormat);
     33int writeToPasteboard(PasteboardRef hPasteboard, uint64_t idOwnership, void const *pv, uint32_t cb, uint32_t fFormat);
    3434
    3535#endif /* !VBOX_INCLUDED_SRC_SharedClipboard_darwin_pasteboard_h */
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette