VirtualBox

Changeset 61658 in vbox


Ignore:
Timestamp:
Jun 10, 2016 2:20:55 PM (9 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
108012
Message:

GuestHost/SharedClipboard: coding fixes and fix for a potential memory leak

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/GuestHost/SharedClipboard/x11-clipboard.cpp

    r61589 r61658  
    469469        {
    470470            if (   (clipVBoxFormatForX11Format(format) == VBOX_SHARED_CLIPBOARD_FMT_HTML)
    471                     && enmBestHtmlTarget < clipRealFormatForX11Format(format))
     471                && enmBestHtmlTarget < clipRealFormatForX11Format(format))
    472472            {
    473473                enmBestHtmlTarget = clipRealFormatForX11Format(format);
     
    11931193 *                         data we are returning
    11941194 */
    1195 static int clipWinHTMLToUtf8ForX11CB(Display *pDisplay, const char* pszSrc,
     1195static int clipWinHTMLToUtf8ForX11CB(Display *pDisplay, const char *pszSrc,
    11961196                                    size_t cbSrc, Atom *atomTarget,
    11971197                                    Atom *atomTypeReturn,
     
    16091609*                    Undefined otherwise.  Optional
    16101610*/
    1611 int  clipUTF16ToWinHTML(RTUTF16* buffer, size_t cb, char **output, uint32_t *outsz)
    1612 {
    1613     Assert(buffer);
     1611int  clipUTF16ToWinHTML(RTUTF16 *pwcBuf, size_t cb, char **ppszOut, uint32_t *pcOut)
     1612{
     1613    Assert(pwcBuf);
    16141614    Assert(cb);
    1615     Assert(output);
    1616     Assert(outsz);
    1617 
     1615    Assert(ppszOut);
     1616    Assert(pcOut);
     1617
     1618    if (cb % 2)
     1619        return VERR_INVALID_PARAMETER;
     1620    size_t cwc = cb / 2;
    16181621    size_t i = 0;
    1619     RTUTF16* p = buffer;
    1620     char* result = NULL;
    1621     size_t resultLen = 0;
    1622     LogRelFlowFunc(("clipUTF16ToWinHTML src= %ls cb=%d i=%i, %x %x\n", buffer, cb, i, output, outsz));
    1623     while (i != cb / 2)
     1622    RTUTF16 *pwc = pwcBuf;
     1623    char *pchRes = NULL;
     1624    size_t cRes = 0;
     1625    LogRelFlowFunc(("clipUTF16ToWinHTML src= %ls cb=%d i=%i, %x %x\n", pwcBuf, cb, i, ppszOut, pcOut));
     1626    while (i < cwc)
    16241627    {
    16251628        /* find  zero symbol (end of string) */
    1626         for (; i < cb / 2 && buffer[i] != 0; i++);
    1627         LogRelFlowFunc(("skipped nulls i=%d cb/2=%d\n", i, cb / 2));
     1629        for (; i < cwc && pwcBuf[i] != 0; i++)
     1630            ;
     1631        LogRelFlowFunc(("skipped nulls i=%d cwc=%d\n", i, cwc));
    16281632
    16291633        /* convert found string */
    1630         char* cTmp = NULL;
    1631         size_t sz = 0;
    1632         int rc = RTUtf16ToUtf8Ex(p, cb / 2, &cTmp, p - buffer, &sz);
    1633         LogRelFlowFunc(("utf16toutf8 src= %ls res=%s i=%i\n", p, cTmp, i));
    1634         if (!RT_SUCCESS(rc))
     1634        char *psz = NULL;
     1635        size_t cch = 0;
     1636        int rc = RTUtf16ToUtf8Ex(pwc, cwc, &psz, pwc - pwcBuf, &cch);
     1637        LogRelFlowFunc(("utf16toutf8 src= %ls res=%s i=%i\n", pwc, psz, i));
     1638        if (RT_FAILURE(rc))
     1639        {
     1640            RTMemFree(pchRes);
    16351641            return rc;
     1642        }
    16361643
    16371644        /* append new substring */
    1638         result = (char*)RTMemRealloc(result, resultLen + sz + 1);
    1639         if (result == NULL)
     1645        char *pchNew = (char*)RTMemRealloc(pchRes, cRes + cch + 1);
     1646        if (!pchNew)
    16401647        {
    1641             RTStrFree(cTmp);
    1642             cTmp = NULL;
     1648            RTMemFree(pchRes);
     1649            RTStrFree(psz);
    16431650            return VERR_NO_MEMORY;
    16441651        }
    1645         memcpy(result + resultLen, cTmp, sz + 1);
    1646         LogRelFlowFunc(("Temp result res=%s\n", result + resultLen));
     1652        pchRes = pchNew;
     1653        memcpy(pchRes + cRes, psz, cch + 1);
     1654        LogRelFlowFunc(("Temp result res=%s\n", pchRes + cRes));
    16471655
    16481656        /* remove temporary buffer */
    1649         RTStrFree(cTmp);
    1650         resultLen += sz + 1;
     1657        RTStrFree(psz);
     1658        cRes += cch + 1;
    16511659        /* skip zero symbols */
    1652         for (; i < cb / 2 && buffer[i] == 0; i++);
     1660        for (; i < cwc && pwcBuf[i] == 0; i++)
     1661            ;
    16531662        /* remember start of string */
    1654         p += i;
    1655     }
    1656     *output = result;
    1657     *outsz = resultLen;
     1663        pwc += i;
     1664    }
     1665    *ppszOut = pchRes;
     1666    *pcOut = cRes;
    16581667
    16591668    return VINF_SUCCESS;
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